Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular Expressions in JS

    Code:
    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:

    Code:
    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.
    Last edited by ]|V|[agnus; 07-17-2004 at 10:05 AM.

  • #2
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does i validate other addresses correctly? Like http://www.domain.com/ or http://pictures.ebay.com?

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, it does not.

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Maybe it doesn't support :alnum: try chaning it for \w.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •