phone number
I want to validate phone number and check whether the phone number has numeric digits or not
Show to me step by step how to do it
Show to me step by step how to do it
/^\d+$/.^ asserts the position at the start of the string.\d+ matches one or more numeric digits.$ asserts the position at the end of the string.phoneNumber as input.test method of the regex to check if the phoneNumber contains only numeric digits.true if the phone number is valid and false otherwise.validatePhoneNumber function and check if a given phone number is valid./^\d+$/\d+$validatePhoneNumberfunction validatePhoneNumber(phoneNumber) {
// Regular expression to check if the phone number contains only numeric digits
const phoneRegex = /^\d+$/;
// Test the phone number against the regex
if (phoneRegex.test(phoneNumber)) {
return true; // The phone number is valid
} else {
return false; // The phone number is invalid
}
}
// Example usage
const phoneNumber = "1234567890";
const isValid = validatePhoneNumber(phoneNumber);
if (isValid) {
console.log("The phone number is valid.");
} else {
console.log("The phone number is invalid.");
}