Web Analytics Made Easy -
StatCounter validating numeric value in a textfield - CodingForum

Announcement

Collapse
No announcement yet.

validating numeric value in a textfield

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • validating numeric value in a textfield

    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:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<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;
    			}
    		</script>
    	</head>
    	<body>
    		<form id="form1">
    			<fieldset>
    				<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()" />
    			</fieldset>
    		</form>
    	</body>
    </html>
    Any help would be greatly appreciated.
    Thanks.

  • #2
    Take out onclick="checkZip()" from the submit button and put in the opening form tag onsubmit="return checkZip()"
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

    Comment


    • #3
      Perfect. Thank you very much.

      Comment

      Working...
      X