Validate (Australian) Phone Numbers in Javascript
Solution 1
Here is a regex that I would recomment
var pattern = /^0[0-8]\d{8}$/g;
So input must start with 0, and followed by a digit and it must be one between 0-8. Then it must have 8 more digit numbers.
Valid phone number examples:
0010293999 (ok)
0110293999 (ok)
0210293999 (ok)
0910293999 (nope)
//Implementation
........
var phoneNumber =document.forms["form3"]["phone"].value;
var phonePattern = /^0[0-8]\d{8}$/g;
//phone number is not valid. Please notice that you don't need to check if it's empty or null since Regex checks it for you anyways
if (!phoneNumber.test(phonePattern))
{
alert("Please Enter your Phone or Mobile Number - Preferably Phone Number");
return false;
}
..........
---- Edit
........
var phoneNumber =document.forms["form3"]["phone"].value;
var phonePattern = /^0[0-8]\d{8}$/g;
//phone number is not valid. Please notice that you don't need to check if it's empty or null since Regex checks it for you anyways
if (!phonePattern.test(phoneNumber))
{
alert("Please Enter your Phone or Mobile Number - Preferably Phone Number");
return false;
}
..........
Solution 2
Thanks to Paul Ferrett for this (php not js, but the regex should translate):
<?php
function validate_phone($number) {
$number = preg_replace('/[^\d]/', '', $number);
return preg_match('/^(0(2|3|4|7|8))?\d{8}$/', $number)
|| preg_match('/^1(3|8)00\d{6}$/', $number)
|| preg_match('/^13\d{4}$/', $number);
}
NB: "MIT License"
Webnerdoz
A SEO lover, web designer, CSS lover, Google Adwords qualified professional, marketing graduate, out of the box thinker, enthusiastic, commercially grounded, Business growth catalyst. I enjoy blogging about SEO and Webmaster related stuff. Xtras - I blog and support the open source. I am into sports, play soccer and basketball in a regular basis. other Passions: philosophy, traveling, gym, networking, socializing, beach, mentoring and experiencing new things.
Updated on July 22, 2022Comments
-
Webnerdoz almost 2 years
I need to validate Australian phone numbers (e.g.
02[3-9]\d{7}
or07[3-9]\d{7}
or04[\d]{8}
) in JavaScript.Requirements:
- must be 10 digits
- no commas
- no dashes
- no + in front
- must begin with 0
At the moment I can validate required fields and email address but I want to add phone number validation.
<html> <head> <script type="text/javascript"> function validateForm() { var x=document.forms["form3"]["name"].value; if (x==null || x=="") { alert("Name must be filled out"); return false; } var s=document.forms["form3"]["phone"].value; if (s==null || s=="") { alert("Please Enter your Phone or Mobile Number - Preferably Phone Number"); return false; } var s=document.forms["form3"]["email"].value; if (s==null || s=="") { alert("Please Enter a valid email address"); return false; } var k=document.forms["form3"]["email"].value; var atpos=k.indexOf("@"); var dotpos=k.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=k.length) { alert("Email Address is Not Valid. Please provide your correct email address."); return false; } } </script> </head> <body> <form action="/thank-you.php" name="form3" method="post" onsubmit="return validateForm();" > Your name* <input type="text" name="name" /> Phone number* <input type="text" name="phone" /> Email* <input type="text" name="email" /> <input type="submit" value="sumbit" name="submit" class="button" onclick="javascript:return validateMyForm();" /><input type="reset" value="Reset" class="resetbutton" /> </form> </body> </html>
Can someone help out?
-
Webnerdoz about 11 yearsNah didn't work. just added your code but it sends the web message without validating all fields.
-
Webnerdoz about 11 yearsthanks. This one works like a charm. The problem is the error message for phone field is different from other JavaScript error messages...
-
Webnerdoz about 11 yearsNah on second thought, this one doesn't work when filling the form through the IE browsers.
-
Jason about 11 yearsI thought you are asking about regex for phonenumber?
-
Jason about 11 yearsI am sorry, there was a small error from the example code. it must be !phonePattern.test(phoneNumber) instead of !phoneNumber.test(phonePattern)
-
Eric about 11 yearsThere's nothing special about
required pattern=
. That's just two attributes,pattern=
, andrequired
. -
Jason about 11 years
-
Jason about 11 yearsBasically test is not a function of a String =/
-
Webnerdoz about 11 yearsAll good now. Thanks heaps. sorry I am not able to vote you up as I don't have 15 reputation yet. I'll def do it in the future. Thanks again.
-
Luke Stevenson about 8 yearsSome of the provided patterns would not match Australian phone numbers, as per OP question. Numbers starting 05, for instance, would not be valid for Australia, for instance. australia.gov.au/about-australia/facts-and-figures/…