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?

shA.t
  • 16,580
  • 5
  • 54
  • 111
marcusstarnes
  • 6,393
  • 14
  • 65
  • 112

2 Answers2

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

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

Other solutions are not always reliable.

shA.t
  • 16,580
  • 5
  • 54
  • 111
bresleveloper
  • 5,940
  • 3
  • 33
  • 47