I'm just starting to learn JavaScript. I tried validating one of the fields in the code below, but it's not working. I removed all the code that does not speak to this issue (hope that was the right thing to do - I'm also new to DP). The CSS code from the stylesheet for the one <div> is at the very bottom. Would someone please tell me where I'm going wrong? <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <script type="text/javascript"> function validateform() { var x=document.forms["Email"]["Disclaimer"].value; if (x==null || x==" ") { alert ("You must verify that you have read the Disclaimer."); return false; } } </script> </head> <body> <div class="formbox"> <form name="Email" action="http://www.justhost.com/justmail" onsubmit="return validateform()" enctype="multipart/form-data" method="post"> <fieldset> <legend>Email:</legend> <span style="line-height: 150%"> Name: <input type="text" name="Name" /><br /> Email (required): <input type="text" name="mailfrom" /><br /> City: <input type="text" name="City" /><br /> State: <input type="text" name="State" size="2" /> Zip: <input type="text" name="Zip" size="10" /><br /> Phone: <input type="text" name="Phone" /></span><br /><br /> How would you like to be contacted?<br />(check all that apply):<br /> <input type="checkbox" name="Contact 1" value="Contact by email" /> Email <input type="checkbox" name="Contact 2" value="Contact by phone" /> Phone<br /><br /> How can we help you?<br /> Brief description of your legal issue:<br /> <textarea name="Message" rows="10" cols="30"></textarea><br /><br /> The use of the Internet or this form for communication with the firm or any individual member of the firm does not establish an attorney-client relationship. Confidential or time-sensitive information should not be sent through this form.<br /> <input type="checkbox" name="Disclaimer" value="I have read the disclaimer." /> I have read the disclaimer. (required) <input type="hidden" name="sendtoemail" value="mary@marymcmullen.com" /> <input type="hidden" name="redirect" value="http://www.marymcmullen.com/index.html" /> <input type="hidden" name="subject" value="Email from WebForm" /> <input type="submit" value="Send Email" /> </fieldset> </form> </div> </body> </html> .formbox { /* Contact */ width: 320px; background-color: #ffffcc; color: #330033; font-size: 1.15em; text-align: center; position: absolute; top: 225px; right: 80px; z-index: 3; outline: 0px solid black; }
Use the 'document.getElementById' method for accessing the checkBox. Check the solution on this post : http://forums.digitalpoint.com/showthread.php?t=2525963
I haven't read all your code because you didn't bother formatting it in tags, but I can see straight away that you're not validating your e-mails. Simply detecting whether or not the value of the form element is NULL or empty doesn't tell you whether the e-mail is correct. To validate an e-mail address, you should use a regular expression along the lines of: var email = document.getElementById("email").value; if(!email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) { alert("Invalid e-mail format."); } Code (markup): You really also need to validate this e-mail on the server-side as well, because JavaScript can be disabled; though most people don't disable it. In any case, it's better to have extra validation and protection than only having one form of validation, which is easily rendered useless.
He is not trying to validate the e-mail. He wants to check whether the 'Disclaimer' checkbox in the form named 'Email' is checked or not.
Hi Unni . . . Thanks for your help, and you're right . . . I'm not trying to to validate the email address yet because I thought the checkbox might be easier. If I can get this right (it's my first script), I'll try the other. Anyway, below are the changes I made; but I must still be missing something because it's still not working. I tried putting the 'onsubmit="return validateform()"' in the beginning form element and the submit element also with no luck. Can you see where I'm going wrong? <input type="checkbox" id="checkbox" name="Disclaimer" value="I have read the disclaimer." onsubmit="return validateform()" /> I have read the disclaimer. (required) <input type="submit" value="Send Email" /> </fieldset> </form> <script type="text/javascript"> function validateform() { var x=document.getElementById("checkbox").value; if (x==null || x==" ") { alert("You must verify that you have read the Disclaimer."); return false; } } </script> Just now I tried to validate the page, but the validator would not let me use 'onsubmit'. So I tried 'onclick' and put it in the submit element. It validates okay now, but still does not work!!
Hi MaryMach, You should use the 'checked' attribute for checkbox rather than 'value'. Also its better if you use a different id. ''See code below: function validateform() { if (! document.getElementById("myCheckBox").checked) { alert("You must verify that you have read the Disclaimer."); return false; } } Code (markup):
Your information is good. Use the this documentation for the validating Email. Server are detect the valid email address.