1

i want a javascript regex for my input that accept only numbers with 10 or 12 digits. forexample:

1234567890 => True

a12345678u =>False

123456789123 =>True

sj231@ => False

aq123456789t =>False

i very very appreciate if any one can help me.

Spectric
  • 30,714
  • 6
  • 20
  • 43
Ali Ehyaie
  • 1,116
  • 3
  • 13
  • 27

1 Answers1

3

Try: [0-9]{10}([0-9]{2})?

const regex = new RegExp("[0-9]{10}([0-9]{2})?");

console.log(regex.test('1234567890'));
console.log(regex.test('a12345678u'));
console.log(regex.test('123456789123'));
console.log(regex.test('sj231@'));
console.log(regex.test('aq123456789t'));

regex101 explanation:

enter image description here

Spectric
  • 30,714
  • 6
  • 20
  • 43