llputney
asked on
Phone Validation Javascript : How to add a second number?
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.txtHomePh one.value= "";
document.frmMain.txtHomePh one.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.txtHomePh one.value= "";
document.frmMain.txtHomePh one.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.txtHomePh one.value= "";
pp="("+p13+")"+p14+p15;
document.frmMain.txtHomePh one.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.txtHomePh one.value= "";
document.frmMain.txtHomePh one.value= pp;
//obj1.value="";
//obj1.value=pp;
}
}
//}
setTimeout(ValidatePhone,1 00)
}
function getIt(m){
n=m.name;
//p1=document.forms[0].ele ments[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>
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.txtHomePh
document.frmMain.txtHomePh
}
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.txtHomePh
document.frmMain.txtHomePh
}
}
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.txtHomePh
pp="("+p13+")"+p14+p15;
document.frmMain.txtHomePh
//obj1.value="";
//obj1.value=pp;
}
l16=p.length;
p16=p.substring(d2+1,l16);
l17=p16.length;
if(l17>3&&p16.indexOf('-')
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.txtHomePh
document.frmMain.txtHomePh
//obj1.value="";
//obj1.value=pp;
}
}
//}
setTimeout(ValidatePhone,1
}
function getIt(m){
n=m.name;
//p1=document.forms[0].ele
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(
<td class="listlabel" align="right">Work Phone:</td>
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtWorkPhone" value="<%=strWorkPhone%>">
ASKER
I just know that's not going to work because in the javascript, there's a snippet of code that reads:
document.frmMain.txtHomePh one.value= "";
document.frmMain.txtHomePh one.value= pp;
what about having one for txtWorkPhone or txtVerPhone
But I'm gonna go ahead and give your solution a try.
document.frmMain.txtHomePh
document.frmMain.txtHomePh
what about having one for txtWorkPhone or txtVerPhone
But I'm gonna go ahead and give your solution a try.
ASKER
nope...didnt work
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
are you wanting to validate all three phone numbers with one button?
ASKER
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.
Barry62: - Yes, like I said to raj3060, this form has many other fields and uses one Submit button.
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.
Check it out.
ASKER
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?
ASKER
I got it figured out. Thanks for the javascript code!
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtHomePhone" maxlength="13" onclick="javascript:getIt(
<td class="listlabel" align="right">Work Phone:</td>
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtWorkPhone" onclick="javascript:getIt(
<td class="listlabel" align="right">Ver. Phone:</td>
<td class="listitem"><input type="text" size="16" class="cmdbutton" name="txtVerPhone" onclick="javascript:getIt(