Javascript How-ToAsk your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
<script language="javascript">
function Validate()
{
var x = document.form1.txtPhone.value;
var y = document.form1.txtMobile.value;
if(isNaN(x)||x.indexOf(" ")!=-1)
{
alert("Enter numeric value")
return false;
}
if (x.length>8)
{
alert("enter 8 characters");
return false;
}
if (x.charAt(0)!="2")
{
alert("it should start with 2 ");
return false
}
if(isNaN(y)||y.indexOf(" ")!=-1)
{
alert("Enter numeric value")
return false;
}
if (y.length>10)
{
alert("enter 10 characters");
return false;
}
if (y.charAt(0)!="9")
{
alert("it should start with 9 ");
return false
}
}
</script>
+61 (0)2 9323 2800 (Australian industry standard for a landline)
+61 (0) 448 721 381 (Australian industry standard for a mobile)
Different countrys different standards and syntax'es. Therefore why be so anal about how somebody enteres a phone number?
What if javascript is turned off etc....?
What if they have a 1300.... or a 1800... or a 1900... type number (we have these in Aussie) - IMO people over think phone number validation. Is there a specific reason you are insisting on a certain syntax?
Frankley I input phone numbers into text fields to cater for things line + and ( and ) - hey in the end if they cant enter a phone number correctly do you want thier business?
My thoughts exactly, you still won't know if it's a valid number or even someone else's number. How often are you going to ring these people? I would maybe just test that there were no characters other than digits and have a label saying Phone number (digits only) or similar to try to prevent inadvertent typing errors. Surely the format is a presentational issue rather than a data issue? My firm came out with a similar ill conceived idea, demanding date of birth be entered on a registration form with no way of knowing whether it was accurate, we dropped it soon afterwards.