Harinatha Reddy
Harinatha Reddy

Reputation: 155

Regex to validate the alpha numeric which optionally include the hypen or space

Validate the given string which includes the alphanumeric of the length either 6 or 7 (includes space or hyphen only once)

the input string length is either 6 or 7 characters if includes a hyphen or space its length should be 7. Only one space or Hyphen is allowed as a max

Note: the Hyphen or Space is any place in the string and in the below example given few

ex: 
A12B3C    -> Valid 
ACB1234   -> Valid
123H67J   -> Valid
ABC-123   -> Valid
ANV 123   -> Valid 
ABV-12345 -> Not Valid 
ABC 1234  -> Not Valid 
ABC-12    -> NOT Valid
ABC 12    -> Not Valid
ABV--35 -> Not Valid
AV- 345 -> Not Valid  
AV- 124 -> Not Valid 
AV- 124 -> Not Valid 
AV-12 5 -> Not Valid 
AV-12 5 -> Not Valid 

Regex: ^(?![^0-9A-Z]*([0-9A-Z])(?:[^0-9A-Z]*\1)+[^0-9A-Z]*$)(?=.{7}$)[0-9A-Z]+(?:[- ][0-9A-Z]+)?$

failed the cases like ANV123 as its expected 7 characters

Upvotes: 0

Views: 48

Answers (3)

Peter Thoeny
Peter Thoeny

Reputation: 7616

Here is functional code with test input based on your requirement to have 6 or 7 alphanumeric chars, or a pattern of 7 chars where all but one are alphanumeric chars with one interspersed space or dash. In addition, do not allow a pattern of all the same char:

const input = [
  'A12B3C',
  'ACB1234',
  '123H67J',
  'ABC-123',
  'ANV 123',
  'ABV-12345',
  'ABC 1234',
  'ABC-12',
  'ABC 12',
  'ABV--35',
  'AV- 345',
  'AV- 124',
  'AV- 124',
  'AV-12',
  'AV-12',
  'AAAAAA',
  '1111111',
  '1111-11',
  '1112-11',
  'AAAA AA'
];
const re = /^(?:(?!^(.)\1+$)[A-Za-z0-9]{6,7}|(?=^.{7}$)(?!^(.)\2*[ -]\2+$)[A-Za-z0-9]+[ -][A-Za-z0-9]+)$/;

input.forEach(str => {
  console.log(str + ' => ' + re.test(str));
});

Output:

A12B3C => true
ACB1234 => true
123H67J => true
ABC-123 => true
ANV 123 => true
ABV-12345 => false
ABC 1234 => false
ABC-12 => false
ABC 12 => false
ABV--35 => false
AV- 345 => false
AV- 124 => false
AV- 124 => false
AV-12 => false
AV-12 => false
AAAAAA => false
1111111 => false
1111-11 => false
1112-11 => true
AAAA AA => false

Explanation:

  • ^ - anchor at start
  • (?: - non capture group start
  • (?!^(.)\1+$) - negative lookahead for all the same char
  • [A-Za-z0-9]{6,7} - expect 6 or 7 alphanumeric chars
  • | - logical OR
  • (?=^.{7}$) - positive lookahead for exactly 7 chars
  • (?!^(.)\2*[ -]\2+$) - negative lookahead for all the same char with interspersed space or dash
  • [A-Za-z0-9]+[ -][A-Za-z0-9]+ - expect 1+ alphanumeric chars, a space or dash, followed by 1+ alphanumeric chars
  • ) - non capture group end
  • $ - anchor at end

Upvotes: 0

MoonMist
MoonMist

Reputation: 1227

Here's the solution as discussed in the comments of your main post.

^[A-Z0-9]{6,7}$|(?=^.{7}$)^[A-Z0-9]+[- ]?[A-Z0-9]+$

When asking a question, please be sure to outline all of your requirements. Its very hard for people to give a correct solution to questions that they don't know the full context of.

Upvotes: 1

Andy Lester
Andy Lester

Reputation: 93666

From looking at your examples, it sounds like what you want is:

  • Three alphabetic characters [A-Z]{3}
  • An optional space or hyphen [ -]?
  • Three or four digits [0-9]{3,4}

Which looks like this, with the beginning and end of string anchors.

^[A-Z]{3}[ -]?[0-9]{3,4}$

Upvotes: 0

Related Questions