Skip to content

Instantly share code, notes, and snippets.

@4k45hv3rm4
Last active July 17, 2021 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4k45hv3rm4/44ce80c159b436b2bc14f739c08bb176 to your computer and use it in GitHub Desktop.
Save 4k45hv3rm4/44ce80c159b436b2bc14f739c08bb176 to your computer and use it in GitHub Desktop.
Validate Mobile Number function
validateMobile(phone) {
const pattern = /^([0]|\+91)?[6789]\d{9}$/; //Regex Valid for all Indian mobile numbers
if (!isNaN(phone) && phone.match(pattern) ) return true;
return false;
}
},
@abhineet97
Copy link

abhineet97 commented Jul 17, 2021

Nice 👏

Some suggestions:

  • There are some syntax error which will prevent this from running.
  • You can slightly shorten the regexp: /^([0]|\+91)?[6-9]\d{9}$/
  • Instead of phone.match(pattern), use pattern.test(phone) as it returns a boolean value.
  • No need of the if clause, you can simply do this: return (!isNaN(phone) && pattern.test(phone))

@4k45hv3rm4
Copy link
Author

Nice clap

Some suggestions:

* There are some syntax error which will prevent this from running.

* You can slightly shorten the regexp: `/^([0]|\+91)?[6-9]\d{9}$/`

* Instead of `phone.match(pattern)`, use `pattern.test(phone)` as it returns a boolean value.

* No need of the `if` clause, you can simply do this: `return (!isNaN(phone) && pattern.test(phone))`

Got it ! .
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment