Wednesday, April 21, 2010

JS URL Validation

function isValidURL(url){
var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
if(RegExp.test(url)){
return true;
}else{
return false;
}
}

UL1 = document.frm.url.value;

if(!isValidURL(document.frm.url.value))
{
alert("Please enter valid URL");
document.frm.url.focus();
return false;
}

if( UL1.substring(0,7) != "http://" )
{
alert("Please enter URL with http://");
document.frm.url.focus();
return false;
}

No comments:

Post a Comment