0

I need to make my input field excepts 16 digits, and 20 chars. The digits are 1-9. And chars are only

().

So, braces and dots. I can make those braces and dots into my regex to work.

$('#telephone').on('keypress', function (event) {
    var regex = new RegExp("^[0-9]+$");

    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
      event.preventDefault();
      return false;
    }
  });

For example I tried:

var regex = /^[().]{20}[0-9]{16}$/;

And so on, but I guess I am missing a bigger picture. Thank you for your help.

The enterd data should look something like this:

33363748574635274()()....()()()..

And so on. No matter where is dot placed and where is brace placed.

user2450639
  • 196
  • 1
  • 14
  • 1
    Can you give an example of how the entered data looks like? – Luca Kiebel Aug 26 '18 at 15:35
  • 1
    As they, does and braces, are special characters they needs to be escaped, did you try that? We also would like to see what should work and what should not, with some samples – Asons Aug 26 '18 at 15:35
  • I have edit the code with data. – user2450639 Aug 26 '18 at 15:36
  • 1
    It is a better idea to use a lookahead checking the string length first (it would fail a lot of other non-matching strings quicker), then the more precise consuming pattern can follow (see linked dupe thread): `^(?=.{16,20}$)(?:[().]*\d){16}[().]*$`. – Wiktor Stribiżew Aug 26 '18 at 15:56
  • I was able to solve it with ^(?=.{16,20}$)(?:[().]*\d){16}[().]*$ – user2450639 Aug 26 '18 at 15:58

0 Answers0