1

I currently have the following validation expression for one of my asp.net controls, which ensures the user has entered, what we consider to be a valid UK postcode:

ValidationExpression="^\s*([A-Z]{1,2}[0-9R][0-9A-Z]?\s*[0-9][ABD-HJLNP-UW-Z]{2})\s*$"

This works fine if the user enters their postcode using uppercase, but I'd like it to ignore case and am not sure how to incorporate that into the above expression?

0

2 Answers 2

2

I'd like it to ignore case

Activate the ignore case flag by adding this notation to your regex: i.

Your regex would like this one below:

ValidationExpression="/^\s*([A-Z]{1,2}[0-9R][0-9A-Z]?\s*[0-9][ABD-HJLNP-UW-Z]{2})\s*$/i"
Sign up to request clarification or add additional context in comments.

4 Comments

I just tried this, but I get a javascript error whenever the validation is called: 'Invalid regular expression: /^(?i)\s*([A-Z]{1,2}[0-9R][0-9A-Z]?\s*[0-9][ABD-HJLNP-UW-Z]{2})\s*$/: Invalid group'
@marcusstarnes So its a clientside regex. I have updated my regex accordingly.
I tried this clientside regex and whilst it no longer errors, it doesn't validate the reg ex. I've since gone with bresleveloper's approach of just putting lowercase letters in the expression and that seems to be working well. Thanks though.
I also try "[A-Z]*/i" for test but it don't accept lower case characters ;).
1

The only simple solution is to put lowercase letters everywhere, i.e.: [0-9A-Za-z]

Other solutions are not always reliable.

4 Comments

This solution can decrease the readability and the maintenability of the regex.
indeed but (?i) doesnt work everywhere unfortunately
What do you mean by everywhere ??
i tried to do it in my own .net app and it never worked, and since we're talking here is an example how it changes a bit stackoverflow.com/questions/2641236/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.