Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to force only numbers in the. Help

Status
Not open for further replies.

thelke

MIS
Apr 9, 2002
84
0
0
How can I force this phone number format field to only accept numbers?

Thank you.

var n;
var p;
var p1;
function ValidatePhone()
{

p=p1.value
if(p.length==3)
{
pp=p;
d4=p.indexOf('(')
d5=p.indexOf(')')
if(d4==-1)
{
pp="("+pp;
}
if(d5==-1)
{
pp=pp+")";
}
document.frmOlyrepro.Phone.value="";
document.frmOlyrepro.Phone.value=pp;
}
if(p.length>3)
{
d1=p.indexOf('(')
d2=p.indexOf(')')
if (d2==-1)
{
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"
p31=p.substring(4,l30);
pp=p30+p31;
document.frmOlyrepro.Phone.value="";
document.frmOlyrepro.Phone.value=pp;
}
}
if(p.length>5)
{
p11=p.substring(d1+1,d2);
if(p11.length>3)
{
p12=p11;
l12=p12.length;
l15=p.length
p13=p11.substring(0,3);
p14=p11.substring(3,l12);
p15=p.substring(d2+1,l15);
document.frmOlyrepro.Phone.value="";
pp="("+p13+")"+p14+p15;
document.frmOlyrepro.Phone.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);
pp=p19+p17+"-"+p18;
document.frmOlyrepro.Phone.value="";
document.frmOlyrepro.Phone.value=pp;
}
}
setTimeout(ValidatePhone,100)
}
function getPhone(m)
{
n=m.name;
p1=m
ValidatePhone()
}
function testphone(obj1)
{
p=obj1.value
p=p.replace("(","")
p=p.replace(")","")
p=p.replace("-","")
p=p.replace("-","")
if (isNaN(p)==true)
{
alert("Check phone");
return false;
}
}
 
Use this function:

function validInput(val,evt) {
var e=new Object();
e.which=(evt)?evt.which:event.keyCode;
return (e.which>=48||e.which<=57);
}

In conjuction with this onkeydown event handler:

onkeydown=&quot;return validInput(this.value,event);&quot;

Hope that helps!

Happy coding! :)
 
is there a way I can put the code into this current function? I would not know how call 2 different functions on the same text box. New the javascript. Thank you
 
hi thelke,

your testphone() function will work with slight modification:
Code:
function testphone(obj1) {
	p=obj1.value

	for (idx = 0; idx < p.length; idx++) {
		p=p.replace(&quot;(&quot;,&quot;&quot;)
		p=p.replace(&quot;)&quot;,&quot;&quot;)
		p=p.replace(&quot;-&quot;,&quot;&quot;)
		p=p.replace(&quot;-&quot;,&quot;&quot;)
	}
	
	if (isNaN(p)) {
		alert(&quot;Numbers only please.&quot;);
		return false;
	}
}
======================================

if (!succeed) try();
-jeff
 
that did not work.

not sure why, I replaced the other code. Am I doing something wrong?

I am pulling the code this way:
<input type=&quot;text&quot; size=&quot;15&quot; name=&quot;Phone&quot; onFocus=&quot;javascript:getPhone(this)&quot; maxLength=&quot;13&quot;>

and I am referencing the javascript thru a .js file
 
Get rid of the javascript: tag in the onFocus event handler:

<input type=&quot;text&quot; size=&quot;15&quot; name=&quot;Phone&quot; onFocus=&quot;getPhone(this)&quot; maxLength=&quot;13&quot;>

I like jemminger's function, and use something similar to it for this kind of thing myself that's just a matter of style:

function testphone(onenumber)
{
var pnum=onenumber.value;

while (pnum.indexOf('(') > -1) pnum=pnum.replace('(','');
while (pnum.indexOf(')') > -1) pnum=pnum.replace(')','');
while (pnum.indexOf('-') > -1) pnum=pnum.replace('-','');

if (isNaN(pnum))
{
alert(&quot;Numbers only please.&quot;);
return false;
}
return true;
}

 
I figured out what is getting me. You are giving me code to validat that only numberrs are in the field. I have a format script on the field. But the format field does not call the validation function, and I do not know how to call it. I am new to javascript. Here is the code to format the field.

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=&quot;(&quot;+pp;
}
if(d5==-1)
{
pp=pp+&quot;)&quot;;
}
//pp=&quot;(&quot;+pp+&quot;)&quot;;
document.frmOlyrepro.Phone.value=&quot;&quot;;
document.frmOlyrepro.Phone.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+&quot;)&quot;
p31=p.substring(4,l30);
pp=p30+p31;
//alert(p31);
document.frmOlyrepro.Phone.value=&quot;&quot;;
document.frmOlyrepro.Phone.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.frmOlyrepro.Phone.value=&quot;&quot;;
pp=&quot;(&quot;+p13+&quot;)&quot;+p14+p15;
document.frmOlyrepro.Phone.value=pp;
//obj1.value=&quot;&quot;;
//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+&quot;-&quot;+p18;
document.frmOlyrepro.Phone.value=&quot;&quot;;
document.frmOlyrepro.Phone.value=pp;
//obj1.value=&quot;&quot;;
//obj1.value=pp;
}
}
//}
setTimeout(ValidatePhone,100)
}
function getPhone(m)
{
n=m.name;
//p1=document.forms[0].elements[n]
p1=m
ValidatePhone()
}

The getPhone() function is called in the ASP this way

<input type=&quot;text&quot; size=&quot;15&quot; name=&quot;Phone&quot; onFocus=&quot;getPhone(this)&quot; maxLength=&quot;13&quot;>

How do I use any of these validation functions with the format function?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top