jeudi 28 décembre 2017

JavaScript RegExp test returns wrong value

I am solving curious problem right now. I am testing a string with regexp and it returns false eventhough it should return true. I am implementing it in javascript. I have this function

function isPhoneNumberValid(phoneNumber) {
  return /^\+\d{12}$/.test(phoneNumber);
}

phoneNumber is a variable in format of + and 12 numbers after (+421123123123 for example). I've played around with different versions of regexp like /^\+[0-9]{12}$/.

According to sites like http://ift.tt/2AnY9QE my regexp should be working for the format I want, yet it returns false still. When I tried testing this regexp /^\+[0-9]/ it returned true when only +3 was written, I guess the problem is with the numbers count?

Parameter phoneNumber received in the function is correct one so I don't think the mistake is there. Also no combination of modifiers helped.

Here is a log of this function

function isPhoneNumberValid(phoneNumber) {
    console.log('ph:'+phoneNumber);
    console.log(/^\+\d{12}$/.test(phoneNumber));
}

enter image description here

Aucun commentaire:

Enregistrer un commentaire