I have some form numbers I need to validate. I’ve tried multiple attempts but am not getting it right yet. While much is allowed in a form number there are some limits I need to impose:
All of these rules should be enforced:
- A-Z allowed but not required (see bullet 4)
- 0-9 allowed but not required (see bullet 4)
- period (decimal point) and dash, if present, only allowed once per form number . –
- Minimum length is one character and it cannot be a space, dash or period
- multiple spaces are allowed but two spaces may not be next to each other; also no leading or trailing spaces are allowed
This is what I had before but not all the above rules were enforced.
[A-Z0-9]([A-Za-z0-9 -.])*[A-Z0-9]
So these would be examples of valid form numbers under the new requirements:
123
123 456
A1 IL 23 MN
CL-100 2.0
These would be examples of invalid form numbers under the new requirements:
123 456
25! 25
25-IL 30-1
aa bb CC
This should work
There are two parts. The first one
[A-Z0-9]checks for a single character. If it isn’t a single character then there are some exclusion rules(?! )(?!.* $)(?!.* )(?!.*-.*-)(?!.*\..*\.)(?![.-]$)(in order): no beginning with space, no ending with space, no consecutive double spaces, no two-, no two., no single character.or-followed by end-of-string. Then there is the “base” pattern (one or more of)[A-Z0-9 .-]+Note that you’ll have to escape the
\with another\, so\\.