veenuisthebest
06-03-2008, 06:33 PM
Please see the URL validation JavaScript below. You can see it in action here (http://www.vinayaks.com/url_validate.html). It works fine but I'd like some changes in it.
1. http:// must be checked for presence. (Currently its invalid format is accepted like tp://)
2. I want the top level domain in the URL to be something valid like only .com, .co.in, .biz, .info, .org, .us, .in etc. should be allowed. (Currently even .xyz is accepted)
3. I don't want to allow inside URL's like http://www.domain.com/sub/test.html. Only domains and sub-domains should be allowed exactly like http://www.domain.com and http://www.home.domain.com (Currently it allows inside URL's)
<head>
<script type="text/javascript">
<!--
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;
}
}
function checkField(){
var frm = document.frmValidate, error = "";
if(!isValidURL(frm.url.value)){
error += "Please enter a valid URL\n";
}
if(error != ""){
alert(error);
return false;
}else{
return true;
}
}
//-->
</script>
</head>
1. http:// must be checked for presence. (Currently its invalid format is accepted like tp://)
2. I want the top level domain in the URL to be something valid like only .com, .co.in, .biz, .info, .org, .us, .in etc. should be allowed. (Currently even .xyz is accepted)
3. I don't want to allow inside URL's like http://www.domain.com/sub/test.html. Only domains and sub-domains should be allowed exactly like http://www.domain.com and http://www.home.domain.com (Currently it allows inside URL's)
<head>
<script type="text/javascript">
<!--
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;
}
}
function checkField(){
var frm = document.frmValidate, error = "";
if(!isValidURL(frm.url.value)){
error += "Please enter a valid URL\n";
}
if(error != ""){
alert(error);
return false;
}else{
return true;
}
}
//-->
</script>
</head>