In this tutorial we will show you how to detect URL in input using jQuery. When user type the text in textarea and if the text having URL in it then it will display error.
You may also like Convert URL Text Into HTML Link.
CHECK OUT THIS TUTORIAL LIVE DEMO →
To Detect It Takes Only One Step:-
- Make a HTML file and define markup, scripting and styling
Step 1. Make a HTML file and define markup, scripting and styling
We make a HTML file and save it with a name detect_url.html
<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> function check_url() { var regex = /https?:\/\/[\-A-Za-z0-9+&@#\/%?=~_|$!:,.;]*/g; if($('#text').val().match(regex)) { $("#link_label").html("Your Text Contains Link"); } } </script> <style> body { text-align:center; width:100%; margin:0 auto; padding:0px; font-family:helvetica; background-color:#81F7BE; } #wrapper { text-align:center; margin:0 auto; padding:0px; width:995px; } #text { width:300px; height:150px; padding:10px; font-size:16px; } </style> </head> <body> <div id="wrapper"> <div id="text_div"> <textarea id="text" onkeyup="check_url()"; placeholder="Enter Text With URL To Detect"></textarea> <p id="link_label"></p> </div> </div> </body> </html>
In this step we create a textarea for user to enter text it call check_url() function in every keyup event and in check_url() function we create a regular expression to detect the URL and then we get the text from
textarea and match it with regular expression and if URL is present in the text it will display error.
You may also like Extract URL Data Like Facebook Using jQuery.
Thats all, this is how to detect URL in input using jQuery. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.
Latest Tutorials
- Create Animated Skill Bar Using jQuery, HTML And CSS
- Simple And Best Responsive Web Design Using CSS Part 2
- Show Button On Hover Using HTML And CSS
- Material Design Login Form Using jQuery And CSS
- Animated Progress Bar Using jQuery And CSS
- Dynamic Drop Down Menu Using jQuery, Ajax, PHP And MySQL
- Get Website Statistics Using PHP, jQuery And MySQL
- HTML5 Login And Signup Form With Animation Using jQuery
- Facebook Style Homepage Design Using HTML And CSS
- View Webpage In Fullscreen Using jQuery