Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Location
    Pittsburgh, PA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 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
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Location
    Pittsburgh, PA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perfect. Thank you very much.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •