0

I am trying to set a phone field with following requirements:

  • "+" can come if entered as first letter, otherwise not
  • there can only be numbers or spaces
  • there can be no more than 11 numbers

I did this which works fine for numbers and spaces but I am not sure how to restrict 11 numbers.

function validatePhone(phone) {
        var filter = /^(?=.*\d)[\d ]+$/;
        if (filter.test(phone)) {
          return phone;
        } else {
          return phone.slice(0,-1);
}

Edit:

I don't want to count all of the entries as 11. I want to count only numbers and not spaces and total numbers should be 11 or 10

Steve
  • 2,546
  • 8
  • 49
  • 94
  • Possible duplicate of [Limit number of characters allowed in form input text field](https://stackoverflow.com/questions/8545376/limit-number-of-characters-allowed-in-form-input-text-field) – MatejMecka May 16 '18 at 15:43
  • It counts all of the enteries but I want to count only numbers and not spaces. – Steve May 16 '18 at 15:44
  • 1
    Possible duplicate of [regex that allows 5-10 characters but can have spaces in-between not counting](https://stackoverflow.com/questions/41862024/regex-that-allows-5-10-characters-but-can-have-spaces-in-between-not-counting) – freedomn-m May 16 '18 at 16:01

2 Answers2

2

This proposed solution removes all the spaces and then matches the string against optionally starting with + and then strictly 11 digits.

document.querySelector('#phoneNumber1').addEventListener('input', function(e){
  if (/^\+?\d{11}$/.test(e.target.value.replace(/ /g, ''))) {
    console.log('valid');
  } else {
    console.log('invalid');
  }
});
<input type="text" id="phoneNumber1">
Taplar
  • 24,788
  • 4
  • 22
  • 35
  • To make this "no more than 11" `/^\+?\d{1,11}$/` (change the 1 to the minimum required) – freedomn-m May 16 '18 at 16:00
  • I'm waiting on feedback from the user that that's what they want. My original comment to the OP said they could do {0,11} – Taplar May 16 '18 at 16:01
  • Ah - didn't notice they were both from you, up voted anyway :) – freedomn-m May 16 '18 at 16:02
  • Thanks a lot. But when I enter digit, it still throws me an invalid – Steve May 16 '18 at 16:05
  • Right, because it's checking strictly against 11. As freedomn-m mentioned, do you want to evaluate to 'valid' for less than 11? What minimum number of digits do you consider valid? @Steve – Taplar May 16 '18 at 16:06
  • minimum of 10, maximum of 11. – Steve May 16 '18 at 16:08
  • Digits? Or are you including `+` in that count? Because i'm asking explicitly on the number of digits, not including the + @Steve – Taplar May 16 '18 at 16:09
1

Here a function that trims the result after the eleventh number, ignoring spaces

function validatePhone(phoneNumber){
  return /( *\d){0,11}/.exec(phoneNumber)[0]
}

console.log(validatePhone('12345671'))
console.log(validatePhone('12345678901'))
console.log(validatePhone('123456789012'))
console.log(validatePhone('1234 56 78   901'))
console.log(validatePhone('123456  7  89012'))

The regex looks for any number of spaces and then a number, at most eleven times and returns the first match.

You have still to handle the starting plus sign and failures (e.g. no match) but this is a starting point

Francesco
  • 4,052
  • 2
  • 21
  • 29