View Full Version : validating numeric value in a textfield

get activated
10-12-2006, 04:58 PM
I'm having a problem with this script I'm working on. What I want to do is check that a user has entered only numeric characters in a text box. If not, an error message should be displayed telling the user that this field requires only numeric input.

It seems to be hitting that part of the function, but I'm not sure what I'm doing wrong that the form is still posting back. Here is the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<title>numeric input validation</title>
<script type="text/javascript">
function checkZip() {
var zip = document.getElementById("txtZip");
zip = zip.value;
if(isNaN(zip)) {
//alert("Please enter only numbers.");
var error = document.getElementById("error");
error.innerHTML = "Please enter only numbers";
return false;
return true;
<form id="form1">
<legend>Zip Code</legend>
<label for="txtZip">What is your Zip code?</label>
<input type="text" id="txtZip" maxlength="5" /><span id="error"></span><br />
<label for="txtName" id="lblName">Your name:</label>
<input type="text" id="txtName" /><br />
<input type="submit" value="submit" onclick="checkZip()" />

Any help would be greatly appreciated.

Mr J
10-12-2006, 05:52 PM
Take out onclick="checkZip()" from the submit button and put in the opening form tag onsubmit="return checkZip()"

get activated
10-12-2006, 05:55 PM
Perfect. Thank you very much.