Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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
 
Old January 1st, 2008, 08:42 AM
Registered User
 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.


 
Old January 3rd, 2008, 09:07 AM
Authorized User
 
Join Date: Nov 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>

 
Old January 8th, 2008, 03:36 AM
Authorized User
 
Join Date: Jul 2007
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to mujahidkhaleel
Default

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.
 
Old January 8th, 2008, 09:49 AM
Registered User
 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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




 
Old January 8th, 2008, 06:43 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

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
 
Old January 9th, 2008, 05:00 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old January 9th, 2008, 05:25 AM
Authorized User
 
Join Date: Nov 2007
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>


 
Old January 9th, 2008, 06:31 PM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

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
 
Old January 10th, 2008, 01:06 AM
Registered User
 
Join Date: May 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,All Thanks for the replys,

Now Its working fine.


Regards,
dhruthi.


 
Old January 10th, 2008, 02:14 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 1,870
Thanks: 12
Thanked 20 Times in 20 Posts
Send a message via AIM to mat41
Default

Its good practice to post your solution so others may benefit

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting mobile phone number yukijocelyn C# 2005 0 May 28th, 2008 11:43 PM
Mobile number Validation dharmeshtandel C# 2 April 23rd, 2008 01:12 PM
phone number validation debjanib ASP.NET 1.0 and 1.1 Professional 0 June 6th, 2006 12:01 PM
reformat phone number netcrawler Classic ASP Databases 1 November 3rd, 2004 09:01 PM
Phone Number validation help. mar0364 Classic ASP Basics 1 July 25th, 2004 06:39 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.