[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Phone Validation Javascript : How to add a second number?

Posted on 2007-08-02
9
Medium Priority
?
212 Views
Last Modified: 2008-01-09
Hi, I have a javascript that does a phone number validation and it works great! However, I have more than one phone number field on the form and I was wondering if anyone could tell me how I can add a second and third phone number field to fit in with this script.

1st Phone Field (already in place): txtHomePhone
2nd Phone Field (need to add): txtWorkPhone
3rd Phone Field (need to add): txtVerPhone



<!-- Begin
var n;
var p;
var p1;
function ValidatePhone(){
p=p1.value
if(p.length==3){
      //d10=p.indexOf('(')
      pp=p;
      d4=p.indexOf('(')
      d5=p.indexOf(')')
      if(d4==-1){
            pp="("+pp;
      }
      if(d5==-1){
            pp=pp+")";
      }
      //pp="("+pp+")";
      document.frmMain.txtHomePhone.value="";
      document.frmMain.txtHomePhone.value=pp;
}
if(p.length>3){
      d1=p.indexOf('(')
      d2=p.indexOf(')')
      if (d2==-1){
            l30=p.length;
            p30=p.substring(0,4);
            //alert(p30);
            p30=p30+")"
            p31=p.substring(4,l30);
            pp=p30+p31;
            //alert(p31);
            document.frmMain.txtHomePhone.value="";
            document.frmMain.txtHomePhone.value=pp;
      }
      }
if(p.length>5){
      p11=p.substring(d1+1,d2);
      if(p11.length>3){
      p12=p11;
      l12=p12.length;
      l15=p.length
      //l12=l12-3
      p13=p11.substring(0,3);
      p14=p11.substring(3,l12);
      p15=p.substring(d2+1,l15);
      document.frmMain.txtHomePhone.value="";
      pp="("+p13+")"+p14+p15;
      document.frmMain.txtHomePhone.value=pp;
      //obj1.value="";
      //obj1.value=pp;
      }
      l16=p.length;
      p16=p.substring(d2+1,l16);
      l17=p16.length;
      if(l17>3&&p16.indexOf('-')==-1){
            p17=p.substring(d2+1,d2+4);
            p18=p.substring(d2+4,l16);
            p19=p.substring(0,d2+1);
            //alert(p19);
      pp=p19+p17+"-"+p18;
      document.frmMain.txtHomePhone.value="";
      document.frmMain.txtHomePhone.value=pp;
      //obj1.value="";
      //obj1.value=pp;
      }
}
//}
setTimeout(ValidatePhone,100)
}
function getIt(m){
n=m.name;
//p1=document.forms[0].elements[n]
p1=m
ValidatePhone()
}
function testphone(obj1){
p=obj1.value
//alert(p)
p=p.replace("(","")
p=p.replace(")","")
p=p.replace("-","")
p=p.replace("-","")
//alert(isNaN(p))
if (isNaN(p)==true){
alert("Check phone");
return false;
}
}
//  End -->
</script>


Here's the HTML section of the code:

<td class="listlabel" align="right">Home Phone:</td>
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtHomePhone" maxlength="13" onclick="javascript:getIt(this)" value="<%=strHomePhone%>"></td>
                                    
<td class="listlabel" align="right">Work Phone:</td>
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtWorkPhone" value="<%=strWorkPhone%>"></td>
0
Comment
Question by:llputney
  • 5
  • 2
  • 2
9 Comments
 
LVL 8

Expert Comment

by:Barry62
ID: 19620788
<td class="listlabel" align="right">Home Phone:</td>
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtHomePhone" maxlength="13" onclick="javascript:getIt(this)" value="<%=strHomePhone%>"></td>
                                   
<td class="listlabel" align="right">Work Phone:</td>
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtWorkPhone"  onclick="javascript:getIt(this)" value="<%=strWorkPhone%>"></td>

<td class="listlabel" align="right">Ver. Phone:</td>
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtVerPhone"  onclick="javascript:getIt(this)" value="<%=strVerPhone%>"></td>
0
 

Author Comment

by:llputney
ID: 19620812
I just know that's not going to work because in the javascript, there's a snippet of code that reads:

document.frmMain.txtHomePhone.value="";
document.frmMain.txtHomePhone.value=pp;

what about having one for txtWorkPhone or txtVerPhone


But I'm gonna go ahead and give your solution a try.
0
 

Author Comment

by:llputney
ID: 19620820
nope...didnt work
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Accepted Solution

by:
raj3060 earned 2000 total points
ID: 19621107
Try this:

<HTML>
<HEAD>
<TITLE>Format Phone</TITLE>
<SCRIPT>
function FormatTel(n){
      var m = n.value.toString().replace(/[^0-9.]/g,'')      
      if((m.length>0) && (m.length<3)) {
      return m;
      }
      else if(m.length==3){            
            n = "("+m + ")"
            return n;
      }else if((m.length>3) && (m.length<6)){            
            n = "("+m.substring(0,3)+")"+m.substring(3)
            return n;
      }else if((m.length >=6) && (m.length<=10)){      
            n = "("+m.substring(0,3) +")" + m.substring(3,6)+ "-" + m.substring(6)
            return n;
      }else if(m.length>10){      
            n = "("+m.substring(0,3) +")" + m.substring(3,6)+ "-" + m.substring(6,10)
            return n;
      }else{
            return m      
      }
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<P>Home Phone:<input type =text name='PH1' id='PH1' onkeyup="this.value=FormatTel(this);" maxlength='13' onblur= 'this.value=FormatTel(this);if((this.value.length>0) && (this.value.length<10)){this.value="";alert("Incorrect Phone format..")}'/></P>
<P>Work Phone:<input type =text name='PH2' id='PH2' onkeyup="this.value=FormatTel(this);" maxlength='13' onblur= 'this.value=FormatTel(this);if((this.value.length>0) && (this.value.length<10)){this.value="";alert("Incorrect Phone format..")}'/></P>
<P>Ver Phone:<input type =text name='PH3' id='PH3' onkeyup="this.value=FormatTel(this);" maxlength='13' onblur= 'this.value=FormatTel(this);if((this.value.length>0) && (this.value.length<10)){this.value="";alert("Incorrect Phone format..")}'/></P>
<p><INPUT TYPE="button" VALUE="Submit"/></p>
</FORM>
</BODY>
</HTML>
0
 
LVL 8

Expert Comment

by:Barry62
ID: 19621158
are you wanting to validate all three phone numbers with one button?
0
 

Author Comment

by:llputney
ID: 19624685
raj3060: - I already have many other fields on the form and it redirects to another page after a user hits submit to enter more fields.  I would like to stick with the javascript I have and just modify it to make it work with more than one phone number field.

Barry62: - Yes, like I said to raj3060, this form has many other fields and uses one Submit button.
0
 
LVL 11

Expert Comment

by:raj3060
ID: 19627149
It does not matter how many fields you have on the form. Did you even try the code. It's doing exactly what code you posted would do in a better and efficient way.

Check it out.
0
 

Author Comment

by:llputney
ID: 19639194
raj3060: I got it to work with your code, just had to make a few adjustments.  There is only one problem.  If I want to edit a pre-existing phone number, it does not use the format from the javascript and saves without giving an error.  Any reason why?
0
 

Author Comment

by:llputney
ID: 19640203
I got it figured out.  Thanks for the javascript code!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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

834 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