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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem making values dynamic in phone number auto-parse script 1

Status
Not open for further replies.

4midori

Programmer
Jan 15, 2003
9
US
I have a script that validates phone numbers, adding - where needed and auto-advancing after each dash. It works fine, but I need to make the last line dynamic.

function autoPhoneNum(theform,thefield) {
num = eval("window.document."+theform+"."+thefield+".value");
num = num.replace(/^[01]+/, '').replace(/[^0-9]/g, '');
num = num.slice(0,3) + (num.length > 3 ? "-" + num.slice(3,6) : "") + (num.length > 6 ? "-" + num.slice(6,10) : "");
window.document.AddClient.HomePhone.value = num;
}

Right now, the second line makes "num" dynamic, but for the last line I have to hard code the name of the element whose value I am setting. How do I make this field dynamic so I can use the script multiple times on a page?

Thanks,
 
you have the answer in your script already
eval("window.document."+theform+"."+thefield+".value");
_______________________________________________
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
<html>
<head>
<script language=&quot;javascript&quot;>
function autoPhoneNum(thefield) {
num = thefield.value
num = num.replace(/^[01]+/, '').replace(/[^0-9]/g, '');
num = num.slice(0,3) + (num.length > 3 ? &quot;-&quot; + num.slice(3,6) : &quot;&quot;) + (num.length > 6 ? &quot;-&quot; + num.slice(6,10) : &quot;&quot;);
return thefield.value = num;
}

</script>
</head>

<body>
<form name=&quot;frm&quot;>
<input type=&quot;text&quot; name=&quot;phone&quot; onBlur=&quot;return autoPhoneNum(this);&quot;>
</form>
</body>
</html> _______________________________________________
{ str = &quot;sleep is good for you. sleep gives you the energy you need to function&quot;;
ptr = /sleep/gi;Nstr = str.replace(ptr,&quot;coffee&quot;);alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top