ASP.NET - 'web site url' validation in javascript

Asked By Anandh Ramanujam on 07-Dec-10 04:55 AM

How can we validate the 'web site url' in javascript...

Can anybody help me in this regard.

Jatin Prajapati replied to Anandh Ramanujam on 07-Dec-10 04:56 AM
try this function

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
Reena Jain replied to Anandh Ramanujam on 07-Dec-10 05:21 AM

there are two eg for you
function Validate(form) {
  var v = new RegExp();
  if (!v.test(form["URL"].value)) {
    alert("You must supply a valid URL.");
    return false;
<FORM NAME="MyForm" ACTION="MyFile.php" METHOD="POST" onSubmit="return Validate(this);">
<script language="javascript">
function isURL(s) {
  var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
    <input type="text" name="url" size="20" id="url"/>
    <input type="button" value="Validate" onClick="javascript:isURL(document.getElementById('url').value);"/>

in above code will checks the following :

o URL starts with a-z, A-Z (e.g. http, ftp...)
o It is then followed by ://
o next we have more letters, numbers and special chars : a-z, A-Z, 0-9, -, _
o there must be at least one "."
o A URL can contain at it's end part the more special characters and this is why the last part allows for more options such as ?, /, &, %, etc. you may add/remove chars as needed to your code.

Hope this will help you
DL M replied to Anandh Ramanujam on 07-Dec-10 05:22 AM
Try below Sample code

 <asp:TextBox ID="txtWebUrl" />

<asp:Button ID="btnSubmit" OnClientClick=" return validate()" runat="server" Text="Submit" />

Now on the source code of this form in script tag write the following code:

if(document.getElementById("<%=txtWebURL.ClientID %>").value=="")


alert("Web URL can not be blank");

document.getElementById("<%=txtWebURL.ClientID %>").value="http://"

document.getElementById("<%=txtWebURL.ClientID %>").focus();

return false;


var Url="^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"

var tempURL=document.getElementById("<%=txtWebURL.ClientID%>").value;

var matchURL=tempURL.match(Url);



alert("Web URL does not look valid");

document.getElementById("<%=txtWebURL.ClientID %>").focus();

return false;