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

    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Validation of last 2 characters


    I have a requirement to validate a 4 character field. The first two characters must be alphanumeric and the last two characters must also be alphanumeric BUT not contain any of these letters: I, O, U or Q.

    I rarely ever work with regular expressions but think I am able to come close based on what I have read.

    The expression ^([A-Z0-9]{2}\w{2}$) will almost accomplish but does not prevent entering I, O, U or Q in the last 2 characters.

    The expression ^([A-Z]{2}[^IiOoUuQq]{2}$) prevents the unwanted charaters but entry of special characters is allowed.

    Can anyone help combine these two approaches for the last two characters?

    Thanks,

    -Troy
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4304
    Welcome to DevShed Forums, Troy.

    You can use multiple character ranges within a character class to do validate the last two characters:
    Code:
    [0-9a-hj-npr-tv-z]{2}

    Comments on this post

    • ragax agrees : Nice use of multiple letter ranges. :)
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  4. #3
  5. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Hi Troy,

    In agreement with Kravvitz---he showed you a nice example of multiple ranges of letters in a character class.

    Just for the archives, here's another solution that uses a lookahead instead.
    Code:
    ^[A-Z0-9]{2}(?:(?!I|O|U|Q)[A-Z0-9]){2}$
    You have to add whatever Javascript needs to make it case-insensitive. I don't program in JS, but according to RB it's something like this:
    Code:
    boolean foundMatch = subjectString.matches("(?i)^[A-Z0-9]{2}(?:(?!I|O|U|Q)[A-Z0-9]){2}$");
    Wishing you both a fun day.
  6. #4
  7. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,124
    Rep Power
    4304
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  8. #5
  9. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    93
    Kravvitz, you're spot on: I had Java instead of JS in the RB pull-down. You can tell I don't code in Java either! Thank you for pointing out the error and providing a resource about how to set flags in JS regex.

    Trying again, here's the JS code that RB generates for the regex I pasted:
    Code:
    if (/^[A-Z0-9]{2}(?:(?!I|O|U|Q)[A-Z0-9]){2}$/i.test(subject)) {
    	// Successful match
    Again, that's just for the archives, I see no particular reason to use that in preference to Kravvitz's character class. One approach is "say exactly what you want", the other is "say what you don't want".

    Wishing you all a fun day

  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by Kravvitz
    Welcome to DevShed Forums, Troy.

    You can use multiple character ranges within a character class to do validate the last two characters:
    Code:
    [0-9a-hj-npr-tv-z]{2}
    Thanks Kravvitz. Your solution works very well and just what I was looking for. Thanks to all for your other responses too.

IMN logo majestic logo threadwatch logo seochat tools logo