Jump to content

Regular Expression


surreal53
 Share

Recommended Posts

I'm attempting to validate UK national insurance numbers using a regular expression. Everything is working 100% up to the last character which should allow A-D or a space. It allows A-D but for some reason I can't get it to allow a space as well, just putting a space as suggested by various forum posts doesn't work unfortunately. Any suggestions welcome!

var pattern = /^([A-C E G-H J-P R-T W-Z]){1}([A-C E G-H J-N P R-T W-Z]){1}( )?([0-9]){2}( )?([0-9]){2}( )?([0-9]){2}( )?([A-D]){1}?$/;

Link to comment
Share on other sites

It will work if you add a space before the A or after the D in the character class. Spaces are literal in a character class, if you have a space in a character class you're telling it that a space is a valid character for that class. So you should be aware that in a character class like this: [A-C E G-H J-P R-T W-Z] that all of those spaces are saying that a space is a valid character for that class. Remove the spaces if you do not want to match a space for that class. e.g.: [A-CEG-HJ-PR-TW-Z] That will match A through C, or E, or G though H, or J through P, or R through T, or W through Z.

Link to comment
Share on other sites

Thanks for the post, but that was not what I am referring to. I am aware of the spaces between those characters (but it is ignored as I'm working in MS Dynamics CRM). As stated previously, putting spaces in the bracket doesn't make any difference. Everything works, except the last character must allow a space or characters A-D.

([A-D]){1}

Putting:

([ A-D]){1}

doesn't make any difference unfortunately, it still flaggs it as an invalid number. Is there an alternative way of allowing spaces?

Link to comment
Share on other sites

That works in Javascript, I don't know if MS Dynamics CRM uses its own syntax for regular expressions, but it works correctly in regular Javascript. If you want to see for yourself, go here: http://www.regular-expressions.info/javascriptexample.html In the Regexp field, enter this: [A-D]{1} In the subject string, enter uppercase A through D and click Test Match to verify that it matches successfully. If you enter a space in the subject string box it does not match. If you add a space to the pattern either before the A or after the D and enter a space in the subject string box it will match the space. So if normal regular expression syntax doesn't work in your environment then you'll need to find a reference for that environment to figure out what the syntax is supposed to be.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...