JayH
JayH

Reputation: 194

Regex String for allowing 1 character and blank space

A field needs validation to only be able to contain (A|B|C|D|E|F|G|H) or blank

Is anybody sure on a regex for this validation?

Currently have this: ^[A-Ha-h]{1}$

Upvotes: 2

Views: 263

Answers (3)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73452

Use this pattern

@"^(?i)[a-h\s]$"
  • (?i) - Ignore case
  • a-h - A-H, a-h
  • \s - matches white space

Upvotes: 2

p.s.w.g
p.s.w.g

Reputation: 149020

If by 'blank space' you mean a literal space character, try this:

^[A-Ha-h ]$

Or use the the RegexOptions.IgnoreCase and simplify this to

^[A-H ]$

On the other hand, if by 'blank space' you mean an empty string, try this:

^[A-Ha-h]?$

Or with RegexOptions.IgnoreCase:

^[A-H]?$

Upvotes: 2

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

Just a simple change:

 ^[A-Ha-h]?$
          ^

This makes it optional to input the character, thus allowing you to input blank.

Upvotes: 6

Related Questions