Richard Korts
asked on
Check for Numeric Phone Field
I want to use Javascript to check an HTML form field for a legit US phone number.
I want to allow for numbers, -, (, ), and . AND I want the number of digits to be exactly 10.
If the field passes these criteria, it is OK & will be passed on as is.
Is there a simple Javascript or Jquery way to do this?
Thanks
I want to allow for numbers, -, (, ), and . AND I want the number of digits to be exactly 10.
If the field passes these criteria, it is OK & will be passed on as is.
Is there a simple Javascript or Jquery way to do this?
Thanks
you can find any regex and plug it to above code to match your needs...
I used
you can find many regex, just google "10 digit phone number regex"
I used
^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$
^\s* #Line start, match any whitespaces at the beginning if any.
(?:\+?(\d{1,3}))? #GROUP 1: The country code. Optional.
[-. (]* #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(\d{3}) #GROUP 2: The Area Code. Required.
[-. )]* #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(\d{3}) #GROUP 3: The Exchange number. Required.
[-. ]* #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(\d{4}) #Group 4: The Subscriber Number. Required.
(?: *x(\d+))? #Group 5: The Extension number. Optional.
\s*$ #Match any ending whitespaces if any and the end of string.
you can find many regex, just google "10 digit phone number regex"
ASKER
Not so good, rejects this phone # with AND without the dashes.
619-855-9192
619-855-9192
it works with "619-855-9192" says yippuuu :)
ASKER
The jsfiddle fails all the time.
I tried to incorporate your code into my test. As follows.
Does nothing , click test, nothing.
I must have done something wrong.
Thnks
I tried to incorporate your code into my test. As follows.
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<script>
function validPhone(phone_number) {
phone_number = phone_number.replace(/\s+/g, "");
return phone_number.length >= 10 && phone_number.match(^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$);
}
function test() {
if (validPhone($("#phone").text())) {
alert("yippu");
} else {
alert("oops!");
};
}
</script>
</head>
<body>
<input id="phone">
<button onClick="javascript:test();">test</button>
</body>
</html>
Does nothing , click test, nothing.
I must have done something wrong.
Thnks
ASKER
I was using the jsfiddle version, I answered before your 2nd answer.
It doesn't work either.
It doesn't work either.
include jquery
<script type="text/javascript" src="//code.jquery.com/jquery-git.js"></script>
ASKER
Still does not work. Does NOTHING.
Code now
Code now
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="//code.jquery.com/jquery-git.js"></script>
<title>Untitled</title>
<script>
function validPhone(phone_number) {
phone_number = phone_number.replace(/\s+/g, "");
return phone_number.length >= 10 && phone_number.match(^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$);
}
function test() {
if (validPhone($("#phone").text())) {
alert("yippu");
} else {
alert("oops!");
};
}
</script>
</head>
<body>
<input id=phone>
<button onClick="javascript:test();">test</button>
</body>
</html>
:
ASKER
Can someone else come in? This seems to be going nowhere.
why dont you copy paste the code from fiddle...
line 13
if (validPhone($("#phone").te xt())) {
>>>
if (validPhone($("#phone").va l())) {
here is the fiddle link again
https://jsfiddle.net/fhgd4ock/
function validPhone(phone_number) {
phone_number = phone_number.replace(/\s+/g, "");
return phone_number.length >= 10 && phone_number.match(/^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$/);
}
function test() {
if (validPhone($("#phone").val())) {
alert("yippu");
} else {
alert("oops!");
};
}
line 13
if (validPhone($("#phone").te
>>>
if (validPhone($("#phone").va
here is the fiddle link again
https://jsfiddle.net/fhgd4ock/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
And the documentation for the above.Note that it has phoneUs property.Jquery validation plugin
Unless you have specific requirements, you might as well go with the jQuery Validation library that's already been written. As pointed out above, it has phone number validation built-in. There's a demo on the site linked in Comment ID 42136045
ASKER
Perfect. I want to use it in one of a series of other form validations that all use Javascript alerts if an issue and DO NOT submit the form. I like the little blue checkmark & the red outlined box, but can I use my "old" methods as well? Otherwise, I have to convert all my other checks to this style (which is EXCELLENT), don't know if I want to go through the effort. Actually don't know how.
Thanks
Thanks
https://jsfiddle.net/fhgd4ock/
HTML
Open in new window
CODE
Open in new window