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 with validation script

Status
Not open for further replies.
Joined
Sep 25, 2002
Messages
159
Location
US
I have the following code that I am trying to use to automatically place dashes between the area code and numbers of my phone numbers. It works fine, except that when I do not type the right number and my alert box appears, when I click ok, it forwards me to the action page. How do I make it stay on this page when I click ok on the alert box? I thought putting return false would do the trick but it doesn't.

<script language="JavaScript">
//Telephone Script

function addDashes(){
var phone = document.forms["mainform"].id_phone.value;
var path = document.forms["mainform"].id_phone;
var areacode = phone.substring(0,3);
var prefix = phone.substring(3,6);
var lastfour = phone.substring(6,10);
var newnumber = areacode + "-" + prefix + "-" + lastfour;

if(phone.length == 10)
{
path.value = newnumber;
path.disabled =true;
}
else{
alert("Please enter a valid telephone number");
path.focus()
return false
}
}
</script>
 
Show the line of HTML where you call the Javscript.

I'd guess that you aren't calling it in the onsubmit event of your <form>, and using return addDashes(). To properly do that, you need to have return true; at the end of the function, as well.

Lee
 
I think you're right, because I am calling it through onclick in my submit button:

<INPUT type="submit" name="Register" value="Register" id="id_function" onclick="addDashes()">
 
What you have won't stop a submit from taking place. Remove the function call from your button, put
Code:
onsubmit="return addDashes();"
in the <form>

and add

Code:
return true;

at the end of your function.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top