fab5freddy Posted December 7, 2010 Report Posted December 7, 2010 I have recently started working on form validation in my class and have gotten stuck on a few things . The first one is I am unsure how to run a test for presses of the space bar. Here is what I have so far. When I put spaces in the text box it still validates and sends the form through. This are both sections taken out of a working function just these two things do not work. if (document.forms[0].artistIdNumber.value.length == " ") { alert("Artist Id cannot contain spaces."); document.forms[0].artistIdNumber.select(); return false; } Also I am having a problem testing for a positive number as well. The number needs to be greater than zero. Here is my code for that. if (document.forms[0].productWeight < 0) { alert("Need to have a positive number"); return false; }
jstern Posted December 7, 2010 Report Posted December 7, 2010 Im definatley not a javascript guy but this line if (document.forms[0].productWeight < 0) { tells me its looking for less than 0. Try < 1 or == 0
falkencreative Posted December 8, 2010 Report Posted December 8, 2010 1: Here's a very brief demonstration: (pulled primarily from http://stackoverflow.com/questions/2031085/check-if-string-contains-characters-whitespaces-not-only-whitespaces <script type="text/javascript"> function check_input() { if (document.form.test.value.match(/^\s+$/)) { alert("Artist Id cannot contain spaces."); document.form.test.select(); } } </script> <form action="" method="post" name="form"> <input type="text" onblur="check_input()" name="test" /> </form> 2: Here's a different version of your if statement that will work for negative numbers and 0 if (document.forms[0].productWeight <= 0) {
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now