#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Simple validation


    Hi,

    I have some regex that doesnt quite work properly.

    I am trying to specify a format such as:
    TEXT-NUMBER-NUMBER

    which I have
    //var discountRegEx = /^([a-zA-Z]+)-(\d+)-(\d+)$/i;

    However the last number I want to specify a number between min 1 & 100 MAX.

    The second NUMBER I want to be between min 1 & 52 max

    Please can somebody advise.
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    Since you're capturing the numbers anyway, it certainly makes your code more legible if you just check for the ranges after your regexp. You can do it within a regexp, of course. However, since regexps have no concept of numeric values, you have to give an exhaustive description of what's acceptable, e.g. for 1-100 you want either 100 or else a character between 1 and 9 followed by any other digit, or else a single digit between 1 and 9:
    Code:
    (100|[1-9]\d|[1-9])
    For 1-52 you want a '5' followed by a 0, 1 or 2. Alternatively 1 to 4 followed by any other digit. Alternatively a single digit from 1 to 9:
    Code:
    (5[0-2]|[1-4]\d|[1-9])
    I haven't tested them, but it'll be close to that anyway.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0
    Thanks that is really helpful!

    I have got the last bit working so nothing over 100 but cant seem to slot the 52 bit in.

    My regex is like so:
    ([a-zA-Z]+)-(5[0-2]|[1-4]\d|[1-9]+)-(100|[1-9]\d|[1-9]+)

    But it still allows this:
    text-53-32

    ALSO (Sorry to be a pain)
    The first bit I would like to allow letters and then a number ([a-zA-Z]+) do you know how i would edit this?

    Thanks again!
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    You put a '+' after the [1-9] in both . Take it back out. Also, you want to anchor that regexp against the start and (more importantly) the end of the string.

    To allow a digit after the text:
    Code:
    [a-zA-Z]+\d
    So the whole thing:
    Code:
    ^([a-zA-Z]+\d)-(5[0-2]|[1-4]\d|[1-9])-(100|[1-9]\d|[1-9])$
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0
    One word... GENIUS!!

    hahaha

    One tiny little thing. I want the first bit of text and a number. The number to not be mandatory?

    I thought that this would suffice?
    ^([a-zA-Z]*\d)-(5[0-2]|[1-4]\d|[1-9])-(100|[1-9]\d|[1-9])$

    It still seems to expect a number afterwards??
  10. #6
  11. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    No, changing the + to a * makes the text optional. To make the \d optional, put a ? after it.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0
    I see I think I need to remember that the symbol goes after the definition as it seems very unlogical to me!

    Thank you for your help as it works perfectly and I should now be able to expand on this.

    Joe

IMN logo majestic logo threadwatch logo seochat tools logo