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"
4
  • 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' Dec 2, 2013 at 11:00
  • @marcusstarnes So its a clientside regex. I have updated my regex accordingly.
    – Stephan
    Dec 2, 2013 at 11:32
  • 1
    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. Dec 2, 2013 at 18:28
  • I also try "[A-Z]*/i" for test but it don't accept lower case characters ;).
    – shA.t
    May 19, 2015 at 4:17
1

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

Other solutions are not always reliable.

4
  • This solution can decrease the readability and the maintenability of the regex.
    – Stephan
    Dec 2, 2013 at 9:49
  • indeed but (?i) doesnt work everywhere unfortunately Dec 2, 2013 at 9:50
  • What do you mean by everywhere ??
    – Stephan
    Dec 2, 2013 at 9:53
  • 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/… Dec 2, 2013 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.