Loading ...

Regular expression to validate URL | CodeAsp.Net

Regular expression to validate URL

 /5
0 (0votes)

In this blog I will explain how to validate URL with regular expression.

Below is the code snippet .

 

 


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Validate URL</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

</head>

<body>

<div>

<input id="txtURL" type="text" style="width:300px; "/><br />

<input id="btnSubmit" type="submit" value="Check URL" /><br />

<div id="MessageDiv" style="display:none;background-color:#E8E8EA ; width:200px; "></div>

</div>

</body>

<script type="text/javascript" >

$(function()

{

var txtURL = $('#txtURL');

var messageDiv = $('#MessageDiv');

$('#btnSubmit').click(function(e)

{

txtURLValue = txtURL.val();

if (txtURLValue == '')

{

messageDiv.html('URL required');

messageDiv.fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);

txtURL.focus();

}

else

{

if (!IsValidURL(txtURLValue))

{

ShowStatus(messageDiv, 'Enter a valid URL');

txtURL.focus();

}

else

{

ShowStatus(messageDiv,'This is valid URL');

}

}

});

function ShowStatus(control, message)

{

control.html(message);

control.fadeIn(400).fadeOut(400).fadeIn(400).fadeOut(400).fadeIn(400);

}

function IsValidURL(url)

{

var filter = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

return filter.test(url);

}

});

</script >

</html>

 

 

 

Happy Reading.!

 

Comments (no comments yet)

Top Posts