Hi there, I'm sure it's something simple, but I can't find it!
Basically, little validation code for my form. I'm attempting to check it nothing has been entered in a text box.
The check-box part works, but I can't get the other validation statements to work!
Code:function validate(form) { if(!document.orderForm.accept.checked){ alert("You must accept the terms and conditions of sale before proceeding."); return false; } else if(document.getElementById('firstName').value == NULL){ alert ("You need to enter your first name."); return false; } else if(document.getElementById('lastName').value == NULL) { alert ("You need to enter your last name."); return false; } else { return true; } }Thanks in advance!HTML Code:<form action="orderProc.php" name="orderForm" method="post" onSubmit='return validate(this)'> <p class="contentText"><b>Contact Details<br /><br /></b>First Name: </p> <input type="text" id="firstName" name="firstName" size="20" maxlength="40" /><br /><br /> <p class="contentText">Last Name: </p> <input type="text" name="lastName" size="20" maxlength="40" /><br /><br /> <p class="contentText"> <input type="checkbox" name="accept" value="no"> I accept the <a class="ip" href="http://sflmaterials.co.uk/TC.pdf" style="text-decoration:underline;" target="_blank">terms and conditions of sale.</a></p><br /><br /> <input type="submit" name="placeOrder" value="Place Order" /> </form>
Last edited by so1i; 11-23-2009 at 04:34 AM. Reason: EDIT: Sorry about typo in title!
A few points
1. You've not given your lastName input an id
2. Javascript is case sensitive. You should be using null rather than NULL
3. If no text is entered in the text input then it's value is an empty string (in firefox at least) and an empty string isn't null. If you are only checking to make sure the text input isn't empty then you are better off using
[highlight="javascript"]
if (document.getElelementById('firstname').value == "")
[/highlight]
If there's a new way, I'll be the first in line.
But, it better work this time.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks