I want to validate my textbox for website url using javascript. Please give me code or code snippet for it so I can use it. Is there any other way to validate it than the javascript?
Please advice.
I want to validate my textbox for website url using javascript. Please give me code or code snippet for it so I can use it. Is there any other way to validate it than the javascript?
Please advice.
Validation should not be done with Javascript because a user can disable Javascript if he or she wants to - validation should be processed server-side.
However it is possible to do so with Javascript.
The variable "result" either shows true or false, whether the input source is a valid URL.Code:var input = document.getElementById("myfield"); var url_match = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/; var result = url_match.test(input);
source >> i could not insert itCode: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; } }
If I were you I would get the function run at onblur event.
Just keep in mind that such things are better off being done server-side (like with PHP) because users can turn off Javascript if they want to.
Good point, mburt. I know my son uses NoScript for FireFox all the time. He's always on about how great it is.
Please press the "Thanks" Button in posts you like, don't post to say "Thanks".
Your son, does web design run in the family? haha thats awesome :P
Of course, validating a post via server side language is crucial, but truning off the javascript is not so good idea in my opinion, because a lot of function of various websites will be blocked this way.
Regards
Zoli
there are many code.. one of them is....
function checkURL(value) {
var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
if(urlregex.test(value))
{
return(true);
}
return(false);
}
if above regular expression does not work then...
change the regx to " ^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}[0-9A-Za-z\.\-]*\.[0-9A-Za-z\.\-]*$ "
try it..!!
Bookmarks