js regex for phone number code example
Example 1: js regex validate phone number
My regex of choice is:
/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im
Valid formats:
(123) 456-7890
(123)456-7890
123-456-7890
123.456.7890
1234567890
+31636363634
075-63546725
Example 2: javascript regex french phone number
function regexPhoneNumber(str) {
const regexPhoneNumber = /^((\+)33|0)[1-9](\d{2}){4}$/;
if (str.match(regexPhoneNumber)) {
return true;
} else {
return false;
}
}
let phoneNumber = "+33777777777";
console.log(regexPhoneNumber(phoneNumber));
Example 3: phone number validation javascript
function telephoneCheck(str) {
var regex = /^(1\s?)?(\(\d{3}\)|\d{3})[\s\-]?\d{3}[\s\-]?\d{4}$/;
return regex.test(str);
}