SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with a regexp

    hi guys and gals

    i wonder if sny one can help with whats seems like a simple problem.

    i have a regexp which is working fine:
    ^[\w\s/,/.\d-]{2,50}$

    ...it excepts alphanumberics, commas, stops and spaces. great!

    i would also like it to pass if the field is blank - as in the field has nothing in it and is ignored by the user.

    im trying to work out how to say the above regexp or blank...

    ...something like:

    ^([\w\s/,/.\d-]{2,50})|("")$
    ^([\w\s/,/.\d-]{2,50})|(/ )$
    ^([\w\s/,/.\d-]{2,50})|()$
    ^([\w\s/,/.\d-]{2,50})|null$
    ^([\w\s/,/.\d-]{2,50})|(null)$
    ^([\w\s/,/.\d-]{2,50})|(\0)$

    none of these work - any ideas??

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code regex:
    (^[\w\s/,/.\d-]{2,50}|$)

    Just including a pipe (or) worked for me in Expresso (regex program) and returned null as a group match.


  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks gRoberts,

    its a shame that doesnt work.

    if you do that, you can enter anything! this renders the whole regex useless!!

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how are you using it? From what you have said, it makes me think your using it to validate a single key press? If so, it will never work (well it doesn't look that way) as it's checking for at least 2 and a max of 50 chars.

    if you have nothing in the first place, it will fail.


  5. #5
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi g,

    sorry, but youre way off.

    the reg is actually used to validate a text input field onblur.
    an address field to be precise.

    but i also want the user to be able to tab threw the field and not have to fill it in - you know like the 3rd address line on a form - this may not apply to the user but any inut should be tested.

    i think ill get round it with a bit of logic, but i was hoping a smart regex could take care of this.

    thanks any way

  6. #6
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the easiest way is to check the length before checking with regex.

    Code javascript:
    if(el.value.length == 0 || el.value.match(/[\w\s/,/.\d-]{2,50}/)) {
                        alert('everythings ok');
                    } else {
                        alert('somethings wrong');
                    }



Bookmarks

Posting Permissions

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