|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Need help validating Australian phone numbers
Hi guys, I'm having some trouble validating Australian phone numbers (both land line and Mobile)
I'm using to following reg expression which i got from:http://regexlib.com/REDetails.aspx?regexp_id=1248 Code:
^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$
But when I open the JavaScript Console in Firefox i get: Error: syntax error Line: 31, Column: 56 As I'm using an external file i've posted the contents below: Code:
function validate_form ( )
{
valid = true;
if ( document.contact.first_name.value == "" )
{
alert ( "Please fill in the 'First Name' box." );
valid = false;
}
if ( document.contact.sur_name.value == "" )
{
alert ( "Please fill in the 'Surname' box." );
valid = false;
}
if ( document.contact.address.value == "" )
{
alert ( "Please fill in the 'Address' box." );
valid = false;
}
if ( document.contact.phone_number.value == "" )
{
alert ( "Please fill in the 'Phone Number' box." );
valid = false;
}
else if ( document.contact.phone_number.value.search(^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$) ==-1)
{
alert ( "Phone Number is in an invalid format." );
valid = false;
}
if ( document.contact.email.value == "" )
{
alert ( "Please fill in the 'Email' box." );
valid = false;
}
else if ( document.contact.email.value.search(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/) ==-1)
{
alert ( "Email is in an invalid format." );
valid = false;
}
return valid;
}
I'm new to JavaScript so any help you provide would be much appreciated Thankyou in advance. |
|
#2
|
||||
|
||||
|
what do your phone #'s look like??
|
|
#3
|
||||
|
||||
|
You need to enclose your regular expressions in quotes, so replace
Code:
^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$
Code:
"^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$"
and Code:
/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/ Code:
"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
__________________
This is my code. Is it not nifty? "The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools." ---Douglas Adams Join the Itsacon fanclub! Zero Tolerance: Spammers banned so far: 278
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > Need help validating Australian phone numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|