...

View Full Version : Regular Expressions in JS



]|V|[agnus
07-17-2004, 11:02 AM
var validWWW = new RegExp("^http://([[:alnum:]._%-]+\.)?[[:alnum:]._%-]+\.[[:alnum:]._%-]{2,4}/?$");
if (!w.match(validWWW)) {
e[e.length] = "The Web site address you entered (" + w + ") is invalid. Example: http://www.domain.com/";
}


"e" is an array that holds error messages. "w" is the passed form value. The pattern above seems to work as intended with ColdFusion, and reject anything but strings with "http://(optional subdomain).domain.(2-4 character suffix)(optional trailing slash)

Now, setting aside that this doesn't account for the less likely but possible chance of something like "http://www.angelfire.com/~users/boobar835/cool%20page.htm", why might it also be rejecting something like "http://domain.com" in JavaScript? I tried setting the patter this way too with the same result:



var validWWW = /^http:\/\/([[:alnum:]._%-]+\.)?[[:alnum:]._%-]+\.[[:alnum:]._%-]{2,4}\/?$/;


I chose the top one because I don't have to escape the slashes then, and the pattern reads clearer. Anyhoo, yes... I am confused why the same pattern works for one and not the other. Is there a difference in their interpretation I should be aware of? PHP also handles the pattern as intended it seems.

trib4lmaniac
07-17-2004, 11:28 PM
Does i validate other addresses correctly? Like http://www.domain.com/ or http://pictures.ebay.com?

]|V|[agnus
07-18-2004, 05:06 AM
No, it does not. :(

trib4lmaniac
07-19-2004, 03:08 PM
Maybe it doesn't support :alnum: try chaning it for \w.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum