Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 1st, 2008, 08:42 AM
Registered User
 
Join Date: May 2007
Location: Mysore, Karnataka, India.
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.


Reply With Quote
  #2 (permalink)  
Old January 3rd, 2008, 09:07 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
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>

Reply With Quote
  #3 (permalink)  
Old January 8th, 2008, 03:36 AM
Authorized User
 
Join Date: Jul 2007
Location: Bangalore, Karnataka, India.
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.
Reply With Quote
  #4 (permalink)  
Old January 8th, 2008, 09:49 AM
Registered User
 
Join Date: May 2007
Location: Mysore, Karnataka, India.
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




Reply With Quote
  #5 (permalink)  
Old January 8th, 2008, 06:43 PM
Friend of Wrox
Points: 6,478, Level: 34
Points: 6,478, Level: 34 Points: 6,478, Level: 34 Points: 6,478, Level: 34
Activity: 14%
Activity: 14% Activity: 14% Activity: 14%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,836
Thanks: 12
Thanked 16 Times in 16 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
Reply With Quote
  #6 (permalink)  
Old January 9th, 2008, 05:00 AM
joefawcett's Avatar
Wrox Author
Points: 9,731, Level: 42
Points: 9,731, Level: 42 Points: 9,731, Level: 42 Points: 9,731, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,068
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)
Reply With Quote
  #7 (permalink)  
Old January 9th, 2008, 05:25 AM
Authorized User
 
Join Date: Nov 2007
Location: , , .
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>


Reply With Quote
  #8 (permalink)  
Old January 9th, 2008, 06:31 PM
Friend of Wrox
Points: 6,478, Level: 34
Points: 6,478, Level: 34 Points: 6,478, Level: 34 Points: 6,478, Level: 34
Activity: 14%
Activity: 14% Activity: 14% Activity: 14%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,836
Thanks: 12
Thanked 16 Times in 16 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
Reply With Quote
  #9 (permalink)  
Old January 10th, 2008, 01:06 AM
Registered User
 
Join Date: May 2007
Location: Mysore, Karnataka, India.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,All Thanks for the replys,

Now Its working fine.


Regards,
dhruthi.


Reply With Quote
  #10 (permalink)  
Old January 10th, 2008, 02:14 AM
Friend of Wrox
Points: 6,478, Level: 34
Points: 6,478, Level: 34 Points: 6,478, Level: 34 Points: 6,478, Level: 34
Activity: 14%
Activity: 14% Activity: 14% Activity: 14%
 
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,836
Thanks: 12
Thanked 16 Times in 16 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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting mobile phone number yukijocelyn C# 2005 0 May 29th, 2008 12:43 AM
Mobile number Validation dharmeshtandel C# 2 April 23rd, 2008 02:12 PM
phone number validation debjanib ASP.NET 1.0 and 1.1 Professional 0 June 6th, 2006 01: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 07:39 AM



All times are GMT -4. The time now is 04:35 PM.


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