// program to validate the phone number
function validatePhone(num) {
// regex pattern for phone number
const re = /^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/g;
// check if the phone number is valid
let result = num.match(re);
if (result) {
console.log('The number is valid.');
}
else {
let num = prompt('Enter number in XXX-XXX-XXXX format:');
validatePhone(num);
}
}
// take input
let number = prompt('Enter a number XXX-XXX-XXXX');
validatePhone(number);
// Commonly used regular expression
// Check 2-9 characters, false if not matched, true if matched
const validateName = (name) => {
const reg = /^[u4e00-u9fa5]{2,9}$/;
return reg.test(name);
};
// Verify phone number
const validatePhoneNum = (mobile) => {
const reg = /^1[3,4,5,6,7,8,9]d{9}$/;
return reg.test(mobile);
};
// Check passwords consisting of 6 to 18 uppercase and lowercase alphanumeric underscores
const validatePassword = (password) => {
const reg = /^[a-zA-Z0-9_]{6,18}$/;
return reg.test(password);
};
/^[+]?[(]?[0-9]{3}[)]?[-s.]?[0-9]{3}[-s.]?[0-9]{4,6}$/im
^(?!.*911.*d{4})((+?1[/ ]?)?(?![(. -]?555.*)( ?[2-9][0-9]{2} ?) ?|(+?1[./ -])?[2-9][0-9]{2}[./ -]?)(?!555.?01..)([2-9][0-9]{2})[./ -]?([0-9]{4})$