Advertisements
Requirement is : Field is made of 1 alpha character followed by 15 alpha numeric characters including maximum 2 hyphens (-)
Regex:^(?!^(.)\\1*$)([A-Z]{1}(?=[A-Z0-9-]{2,15}$)[A-Z0-9]*(?:\\-[A-Z0-9]*){0,2}[A-Z0-9]+$)
Above Regex is working fine but as the requirement it’s not allowed the duplicate numbers like 1) N000000000000000 2)N000000-0000-0
the above 2 values not to allow as its has the same digit as "0".
>Solution :
You can use
^[A-Z](?!\D*(\d)(?:\D*\1)+\D*$)(?=.{2,15}$)[A-Z0-9]*(?:-[A-Z0-9]*){0,2}[A-Z0-9]+$
See the regex demo. Inside the code, in your string literals, make sure you escape backslashes twice.
Details:
^
– start of string[A-Z]
– an uppercase ASCII letter(?!\D*(\d)(?:\D*\1)+\D*$)
– the string cannot contain only identical digits(?=.{2,15}$)
– there must be 2 to 15 chars other than line break chars to the right of the current position[A-Z0-9]*
– zero or more ASCII uppercase letters/digits(?:-[A-Z0-9]*){0,2}
– zero, one or two occurrences of-
and then zero or more ASCII uppercas eletters/digits[A-Z0-9]+
– one or more ASCII uppercase letters/digits$
– end of string.
`