Solved

validation of phone number

Posted on 2007-04-05
2
494 Views
Last Modified: 2012-08-13
I wrote a code;
This code validate the10 digit telephone number; if the telephone number is incorrect or without a space after typing the area code in () , will generate an error.
I have two problems in this code
(i) In the area code field I want the just the area code without parenthisis()
(ii) It is giving me an alert messaged even if I type the right phone number format.

here is the code you can copy and paste the code and see result
*******************

<script language="javascript">
var n =  new Array(2),  f =  new Array(8),s =  new Array(5),a,b,c,d,e,g,h,i,j,r;


function validateThis(n){
 n = document.form1.fullText.value;
if ((n.charAt(0)=='(') && (n.charAt(4)==')') && (n.charAt(5)==' ') && (n.charAt(9)=='-'))
SplitNum(n);
else
window.alert("Please enter a valid number jo jojo")
}
function SplitNum(n,s,f){
var k=n.split(" "), fir = k[0].length, las = k[1].length , q=k[0];
if( fir == 5 && las == 8){
      var s = k[0];
      var f = k[1];
      a = parseInt(s[1]);
      b = parseInt(s[2]);
      c = parseInt(s[3]);
      d = parseInt(f[0]);
      e = parseInt(f[1]);
      g = parseInt(f[2]);
      h = parseInt(f[4]);
      i = parseInt(f[5]);
      j = parseInt(f[6]);
      r = parseInt(f[7]);
      ValNum();
 var jk=s.split(")");
      document.form1.areaText.value =   jk;
      document.form1.numText.value = f ;
      }
      else window.alert("Enter a valid number, quit playing.")

}
function ValNum(){
if(isNaN(a) || isNaN(b) || isNaN(c) || isNaN(d) || isNaN(e) || isNaN(g) || isNaN(h) || isNaN(i) || isNaN(j) || isNaN(r) )
window.alert("Enter a valid number hey bobo")
}
 
</script>
</head>

<body>
<h2>Test</h2>
<form name="form1" id="form1" method="post" action="">
<TABLE BORDER=2 CELLSPACING=1 CELLPADDING=2 width="30%">
<TR>
<TD ALIGN="center"><p>Enter a phone number</p>
  <p>[In the form (555) 555-5555]</p></TD>
<TD ALIGN="center"><input name="fullText" type="text" id="fullText" /></TD>
</TR>
<TR>
<TD ALIGN="center" COLSPAN="1"><input name="valButt" type="button" id="valButt" value="validate" onClick = "validateThis()"></TD>
</TR>

<TR>
<TD ALIGN="center">Area Code: </TD>
<TD ALIGN="center"><input name="areaText" type="text" id="areaText" /></TD>
</TR>
<TR>
<TD ALIGN="center">Number: </TD>
<TD ALIGN="center"><input name="numText" type="text" id="numText" /></TD>
</TR>
</TABLE>
</form>


0
Comment
Question by:sfareed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
stanscott2 earned 250 total points
ID: 18859587
In this situation, I like to help users.  For the first input, for area, I would do this:

<input name="areaText" type="text" id="areaText" onblur="checkArea(this)">

The script would strip out the non-numeric entries, if any, and check for length:

function checkArea(obj) {
   var val = this.value;
   if (val == "") { return; }  // blank, which is okay
   val = val.replace(/\D/g,"");
   this.value = val;
   if (val.length > 0 && val.length != 3) {
      alert("The area code must be three digits.");
      this.focus();
  }
}

This way, the user gets immediate feedback on the entry.

You can always use this test on the final submit:

val.match(/\D/) to determine whether there somehow any NON-numeric characters in the string.
0
 

Author Comment

by:sfareed
ID: 18859657
Thanks for you reply but I can't do that because i have to do the form in the same way as I shown in the code.
This my college project and I can't change the text field.

thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question