 |
Javascript How-To Ask 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

January 1st, 2008, 08:42 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Validation For Phone Number and Mobile Number
Hi Friends,
I need a java Script code for validating a Phone Number(8 Digits- Starting with 2) and Mobile Number(10 Digits- Starts with 9).
Also it should not allow to enter any space, special charcter, as well as charcters.
Here user can enter either phone number or mobile number.
Plese Help in this,
Thanks,
Dhruthi.
|

January 3rd, 2008, 09:07 AM
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This is the javascript function
<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>
|

January 8th, 2008, 03:36 AM
|
Authorized User
|
|
Join Date: Jul 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The better way to do is using regular-expressions. You can find an example in here http://www.devarticles.com/c/a/JavaS...-JavaScript/6/
Hope this helps
Mujahid Khaleel
Web designing, development, E-commerce applications.
|

January 8th, 2008, 09:49 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, friends thanks for the reply.
Still iam unable to achive for writing the code for Phone No Validation.
Here user can enter either 10 digit number(which starts from 9) OR can enter 8 digit number(starts with 2).
following is my code.
-------------------------
<html>
<head>
<title></title>
<script language="javascript">
function Validate()
{
var x = document.sform.phone_no.value;
if(isNaN(x)|| x.indexOf(" ")!=-1){
alert("Enter numeric value");return false; }
if (x.length > 10 || x.length > 8 ){
alert("enter 10 or 8 characters"); return false;
}
if (x.charAt(0)!="9" || x.charAt(0)!="2"){
alert("it should start with 9 or 2 ");
return false
}
}
</head>
<body>
<table width="290" height="83" border="0" bgcolor="#F4FAFF">
<form name="sform" action="search_phone.jsp" method="post">
<tr class="smallfont">
<td width="247" height="24" align="center" valign="middle" class="table_header">New Appointment </td>
<td align="left" valign="middle"> </td>
</tr>
<tr class="smallfont">
<td height="26" align="left" valign="middle"><span class="style4">Phone Number
<input name="phone_no" type="text" id="phone_no" />
</span></td>
<td width="33" align="left" valign="middle"><input type="submit" name="Submit" value="Go" onclick="return Validate()"/></td>
</tr>
<tr class="smallfont">
<td height="25" align="left" valign="middle"> </td>
<td align="left" valign="middle"> </td>
</tr>
</form>
</table>
</body>
</html>
-------------------------------------
But This code is not working in my place.
Is there any way
Regards,
Dhruthi
|

January 8th, 2008, 06:43 PM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
If I am entering a phone number I would enter:
+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?
I hope my 2cents worth is taken constructivly
Wind is your friend
Matt
|

January 9th, 2008, 05:00 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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.
--
Joe ( Microsoft MVP - XML)
|

January 9th, 2008, 05:25 AM
|
Authorized User
|
|
Join Date: Nov 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
You failed to close the </script> tag. This is the improvised code.
<html>
<head>
<title></title>
<script language="javascript">
function Validate()
{
var x = document.sform.phone_no.value;
if(isNaN(x)|| x.indexOf(" ")!=-1){
alert("Enter numeric value");return false; }
if (x.length > 10 || x.length > 8 ){
alert("enter 10 or 8 characters"); return false;
}
if (x.charAt(0)!="9" || x.charAt(0)!="2"){
alert("it should start with 9 or 2 ");
return false
}
}
</script>
</head>
<body>
<table width="290" height="83" border="0" bgcolor="#F4FAFF">
<form name="sform" action="search_phone.jsp" method="post">
<tr class="smallfont">
<td width="247" height="24" align="center" valign="middle" class="table_header">New Appointment </td>
<td align="left" valign="middle"> </td>
</tr>
<tr class="smallfont">
<td height="26" align="left" valign="middle"><span class="style4">Phone Number
<input name="phone_no" type="text" id="phone_no" />
</span></td>
<td width="33" align="left" valign="middle"><input type="submit" name="Submit" value="Go" onclick="return Validate()"/></td>
</tr>
<tr class="smallfont">
<td height="25" align="left" valign="middle"> </td>
<td align="left" valign="middle"> </td>
</tr>
</form>
</table>
</body>
</html>
|

January 9th, 2008, 06:31 PM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
In relation to
if (x.charAt(0)!="9" || x.charAt(0)!="2"){
arnt "9" and "2" are strings?
Wind is your friend
Matt
www.elitemarquees.com.au
|

January 10th, 2008, 01:06 AM
|
Registered User
|
|
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,All Thanks for the replys,
Now Its working fine.
Regards,
dhruthi.
|

January 10th, 2008, 02:14 AM
|
Friend of Wrox
|
|
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
|
|
Its good practice to post your solution so others may benefit
Wind is your friend
Matt
www.elitemarquees.com.au
|
|
 |