The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
JavaScript - Validation of last 2 characters
Discuss Validation of last 2 characters in the Regex Programming forum on Dev Shed. Validation of last 2 characters Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 22nd, 2012, 02:41 PM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 2
Time spent in forums: 19 m 39 sec
Reputation Power: 0
|
|
|
JavaScript - 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
|

February 22nd, 2012, 02:52 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
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}
|

February 22nd, 2012, 11:08 PM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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.
|

February 22nd, 2012, 11:53 PM
|
 |
CSS & JS/DOM Adept
|
|
Join Date: Jul 2004
Location: USA
|
|
|

February 23rd, 2012, 02:41 AM
|
 |
Turn left at the third duck
|
|
Join Date: Dec 2011
Location: Nelson, NZ
|
|
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

|

February 23rd, 2012, 07:47 AM
|
|
Registered User
|
|
Join Date: Feb 2012
Posts: 2
Time spent in forums: 19 m 39 sec
Reputation Power: 0
|
|
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|