All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Detect URL In Input Using jQuery

Last Updated : Jul 1, 2023

IN - jQuery HTML | Written & Updated By - Amruta

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.

Detect URL In Input Using jQuery

To Detect It Takes Only One Step:-

  1. 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.

I hope this tutorial on detect URL in input helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪