WilliamHolmes
06-08-2007, 02:31 PM
I have some javascript code for a URL validation but it doesnt seem to want to work with the following url... (i think its the " ~ " which is causeing the problem)
code
var w = new RegExp();
w.compile("(ftp|http|https)+://[www]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
example url
http://www.yourdomain.ie/~lgb/test_in_image.png
Also, can anyone recode this so that the following url would also work..
example url
http://sub.yourdomain.ie/test_in_image.png
Can anyone help?
Thanks.
A1ien51
06-08-2007, 02:40 PM
add the ~ to the regualr expression than
Eric
WilliamHolmes
06-08-2007, 02:55 PM
Thanks. I tried that but it didnt seem to work. What about the other url using a sub domain rather than www ?
A1ien51
06-08-2007, 03:11 PM
change [www]+ to (www)?
Eric
Philip M
06-08-2007, 03:36 PM
I am not sure how you have derived your regex but it is completely wrong.
e.g. [www] means match w or w or w, not www.
The “safe” characters in a URL are alphanumerics (A to Z, a to z and 0 to 9) and the following special characters: the dollar sign (“$”), hyphen (“-”), underscore (“_”), period (“.”), plus sign (“+”), exclamation point (“!”), asterisk (“*”), apostrophe (“'”), left parenthesis (“(”), and right parenthesis (“)”).
The regex you desire (i.e. validate a URL) is:-
^(ftp|http|https):\/\/www[\w%&\$\-\?\/\.!\+\*\(\)\s=~]+$
& and ? cannot normally appear as part of a URL except in query string.
A tilde ~ is not "safe" character but I have included it. A space should be encoded as %20
What is w.compile supposed to do?
In fact it may be simpler and just as effective to just check the starting characters in the URL up to www.
^(ftp|http|https):\/\/www\.
You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html