Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to create a regular expression to validate the numbers are not same even separated by hyphen (-) or Alpha Numeric

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.
    `
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading