p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   Validation For Phone Number and Mobile Number (http://p2p.wrox.com/showthread.php?t=64920)

dhruthi.ram99 January 1st, 2008 08:42 AM

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.



jijish January 3rd, 2008 09:07 AM

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>


mujahidkhaleel January 8th, 2008 03:36 AM

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.

dhruthi.ram99 January 8th, 2008 09:49 AM

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">&nbsp;</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">&nbsp;</td>
        <td align="left" valign="middle">&nbsp;</td>
      </tr>
    </form>
  </table>
</body>
</html>


-------------------------------------


But This code is not working in my place.
Is there any way

Regards,
Dhruthi





mat41 January 8th, 2008 06:43 PM

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

joefawcett January 9th, 2008 05:00 AM

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)

jijish January 9th, 2008 05:25 AM

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">&nbsp;</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">&nbsp;</td>
        <td align="left" valign="middle">&nbsp;</td>
      </tr>
    </form>
  </table>
</body>
</html>



mat41 January 9th, 2008 06:31 PM

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

dhruthi.ram99 January 10th, 2008 01:06 AM

Hi,All Thanks for the replys,

Now Its working fine.


Regards,
dhruthi.



mat41 January 10th, 2008 02:14 AM

Its good practice to post your solution so others may benefit

Wind is your friend
Matt
www.elitemarquees.com.au


All times are GMT -4. The time now is 11:15 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.