regular expression to validate if the given input is valid Indian mobile number or not 1

regular expression to validate if the given input is valid Indian mobile number or not

//Create a regular expression to validate if the given input is valid Indian mobile number or not

const number = '+919876543210';

function validateMobile(number) {
    const regEx = /^(\+91|0)?( )?(\d{10})$/
    return regEx.test(number)
}

console.log(`is a valid Indian mobile number: ${validateMobile(number)}`)

Here is what the above code is Doing:
1. The first part of the regular expression is ^(\+91|0)?( )? which means that the number can start with +91 or 0 or nothing.
2. The second part of the regular expression is (\d{10})$ which means that the number should end with 10 digits.
3. The third part of the regular expression is ( )? which means that the number can have a space after the country code or not.

Similar Posts