Hi, you need to skip the form field, only if it contains (only numbers) or (numbers, letters, space, brackets, -, +).

The sequence does not matter. (Lax phone number check).

This template works

!/[^0-9a-z\(\)\-\+\s]/i.test('8(911) 000 00-00'); //true 

but the same pattern will miss only letters (as well as only brackets, etc.)

 !/[^0-9a-z\(\)\-\+\s]/i.test('test'); //true 

I guess that it is necessary to use grouping, but how to do it elegantly and concisely, until I understood.

Tell me the template please. Thank.

  • Maybe just ban purely alphabetic strings: /^(?![az]+$)[0-9a-z()+ -]+$/ig ? The question is not completely clear. - Wiktor Stribiżew
  • one
    It is much more elegant for each onChange for the input field to delete everything except numbers. - ReinRaus

2 answers 2

If we assume that the phone still has to start with a number, then the regular expression might look something like this:

 /^[\+\(]{0,1}[\d]+[az\(\)\-\+\s]*/ig 

Rewrote splash58 comments

  • and why not with a bracket - for example, with a city code? - splash58

A regular checker checks that there is at least 1 digit. Other characters requested by the author of the question:

  /^(?=.*[0-9])[0-9a-z \(\)\+-]+$/i