Validate Phone Number With JavaScript

Validate phone number with JavaScript

First off, your format validator is obviously only appropriate for NANP (country code +1) numbers. Will your application be used by someone with a phone number from outside North America? If so, you don't want to prevent those people from entering a perfectly valid [international] number.

Secondly, your validation is incorrect. NANP numbers take the form NXX NXX XXXX where N is a digit 2-9 and X is a digit 0-9. Additionally, area codes and exchanges may not take the form N11 (end with two ones) to avoid confusion with special services except numbers in a non-geographic area code (800, 888, 877, 866, 855, 900) may have a N11 exchange.

So, your regex will pass the number (123) 123 4566 even though that is not a valid phone number. You can fix that by replacing \d{3} with [2-9]{1}\d{2}.

Finally, I get the feeling you're validating user input in a web browser. Remember that client-side validation is only a convenience you provide to the user; you still need to validate all input (again) on the server.

TL;DR don't use a regular expression to validate complex real-world data like phone numbers or URLs. Use a specialized library.

Validate phone number using javascript

JavaScript to validate the phone number:

function phonenumber(inputtxt) {  var phoneno = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;  if(inputtxt.value.match(phoneno)) {    return true;  }  else {    alert("message");    return false;  }}

Validating Phone Numbers Using Javascript

As for your regexp I guess it should be

^\+{0,2}([\-\. ])?(\(?\d{0,3}\))?([\-\. ])?\(?\d{0,3}\)?([\-\. ])?\d{3}([\-\. ])?\d{4}

But in general the presumption is not correct because one might enter something like
++44 20 1234 56789 or +44 (0) 1234 567890
it is better to do something like this

var phone = document.forms["myForm"]["phone"].value;
var phoneNum = phone.replace(/[^\d]/g, '');
if(phoneNum.length > 6 && phoneNum.length < 11) { return true; }

this will assure that entered value has 7 to 10 figures nevertheless what the formatting is. But you have to think about max length for number might be more than 10 as in the sample above.

JS How to validate a mobile phone number

You can use Regular Expressions aka. regex. {Regex Tutorial}

I was bored so I wrote it for you.

validMobileNumber("123"); //.to.equal(false);validMobileNumber("0750617250638"); //.to.equal(false);validMobileNumber("+447712368768724988"); //.to.equal(false);
validMobileNumber("07506172506"); //.to.equal(true);
validMobileNumber("+447506172506"); //.to.equal(true);
validMobileNumber("00447506172506"); //.to.equal(true);
validMobileNumber("07506189foo"); //.to.equal(false);validMobileNumber("00447555123456"); //.to.equal(true);validMobileNumber("+447676111222"); //.to.equal(true);validMobileNumber("07898888643"); //.to.equal(true);validMobileNumber("07766555432"); //.to.equal(true);validMobileNumber("07989765490"); //.to.equal(true);
validMobileNumber("004475551&&&23456"); //.to.equal(false);validMobileNumber("-447676111222"); //.to.equal(false);validMobileNumber("09898888643"); //.to.equal(false);validMobileNumber("+449166555432"); //.to.equal(false);validMobileNumber("cats"); //.to.equal(false);

function validMobileNumber(number) { let regex = /((07)|((\+|00)447)){1}[0-9]{9}\b/, result = regex.test(number); console.log(number, result); return result;}

Validate phone number start with 080, 081, 090, 070, 091

You can use this pattern: ^0([89][01]|70)\d{8}$

This checks for prefixes and a total of 11 digits.

Note: you said a maximum of 11 digits but when I read your code it seems you want exactly 11 digits, the above pattern work for exactly 11 digits. if you want to make it to a maximum of 11 digits you can use this: ^0([89][01]|70)\d{0,8}$

<!DOCTYPE html>
<html>
<body>
<form>
<label for="phone_number">Phone number: </label>
<input type="text" id="phone_number" name="phone_number" pattern="^0([89][01]|70)\d{8}$" title="Maximum length is 11 and Phone number must start with one of 080, 081, 090, 091 or 070 And must be all numeric">
<input type="submit" value="Submit">
</form>
</body>
</html>

Email and phone number validation javascript

Use regex like this

var mailformat = /^\w+([\.-]?\w+)*[@gmail.com]*(\.\w{2,3})+$/;
var email_val = document.getElementById("email").value;
if(!email_val.match(mailformat)){
document.getElementById("message1").innerHTML = "<em> please enter email dsfsdfa</em>";
return false;
}

You can make it more better, like apply validation for empty field before this too.



Related Topics



Leave a reply



Submit