Regex Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreRegex Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 22nd, 2012, 02:41 PM
troyjerkins troyjerkins is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 2 troyjerkins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 22nd, 2012, 02:52 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,832 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 20 h 43 m 39 sec
Reputation Power: 4192
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}
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

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).

Reply With Quote
  #3  
Old February 22nd, 2012, 11:08 PM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
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.
__________________
Regex Tutorial | Latest RegexBuddy Demo

Reply With Quote
  #4  
Old February 22nd, 2012, 11:53 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,832 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 20 h 43 m 39 sec
Reputation Power: 4192

Reply With Quote
  #5  
Old February 23rd, 2012, 02:41 AM
ragax's Avatar
ragax ragax is offline
Turn left at the third duck
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Location: Nelson, NZ
Posts: 93 ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level)ragax User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 24 m 37 sec
Reputation Power: 92
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


Reply With Quote
  #6  
Old February 23rd, 2012, 07:47 AM
troyjerkins troyjerkins is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 2 troyjerkins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > JavaScript - Validation of last 2 characters

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap