ballhogjoni Posted August 8, 2007 Share Posted August 8, 2007 Can someone help me with this code. I have an issue with the last function. The last function is a checkbox not a text field. For some reason It does not show the error if the checkbox is not checked. <script type="text/javascript"> function validateFormOnSubmit(theForm) { var reason = ""; reason += validateUsername(theForm.username); reason += validatepassword(theForm.password); reason += validatepassword_confirmed(theForm.password_confirmed); reason += validateemail(theForm.email); reason += validatei_agree(theForm.i_agree); if (reason != "") { alert("Some fields need correction:\n" + reason); return false; } return true; } function validateEmpty(fld) { var error = ""; if (fld.value.length == 0) { fld.style.background = 'Yellow'; error = "The required field has not been filled in.\n" } else { fld.style.background = 'White'; } return error; } function validateUsername(fld) { var error = ""; var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/; if (fld.value == "") { fld.style.background = 'Yellow'; error = "You didn't enter a username.\n"; } else if ((fld.value.length < 4) || (fld.value.length > 50)) { fld.style.background = 'Yellow'; error = "The username is the wrong length. The username can only have one Capital letter. 50 Characters Max.\n"; } else if (illegalChars.test(fld.value)) { fld.style.background = 'Yellow'; error = "The username contains illegal characters.\n"; } else { fld.style.background = 'White'; } return error; } function validatepassword(fld) { var error = ""; var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/; if (fld.value == "") { fld.style.background = 'Yellow'; error = "You didn't enter a password.\n"; } else if ((fld.value.length < 1) || (fld.value.length > 30)) { fld.style.background = 'Yellow'; error = "The password is the wrong length.\n"; } else if (illegalChars.test(fld.value)) { fld.style.background = 'Yellow'; error = "The password contains illegal characters. 30 Characters Max.\n"; } else { fld.style.background = 'White'; } return error; } function validatepassword_confirmed(fld) { var error = ""; var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ; if (fld.value == "") { fld.style.background = 'Yellow'; error = "You didn't enter a password confirmed.\n"; } else if ((fld.value.length < 1) || (fld.value.length > 30)) { fld.style.background = 'Yellow'; error = "The password confirmed is the wrong length.\n"; } else if (illegalChars.test(fld.value)) { fld.style.background = 'Yellow'; error = "The password confirmed contains illegal characters. 30 Characters Max.\n"; } else { fld.style.background = 'White'; } return error; } function validatepassword_confirmed(fld) { var error = ""; var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ; if (fld.value == "") { fld.style.background = 'Yellow'; error = "You didn't enter a password confirmed.\n"; } else if ((fld.value.length < 1) || (fld.value.length > 30)) { fld.style.background = 'Yellow'; error = "The password confirmed is the wrong length.\n"; } else if (illegalChars.test(fld.value)) { fld.style.background = 'Yellow'; error = "The password confirmed contains illegal characters. 30 Characters Max.\n"; } else { fld.style.background = 'White'; } return error; } function validateemail(fld) { var error = ""; var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ; if (fld.value == "") { fld.style.background = 'Yellow'; error = "You didn't enter an email.\n"; } else if ((fld.value.length < 1) || (fld.value.length > 100)) { fld.style.background = 'Yellow'; error = "The email is the wrong length.\n"; } else if (illegalChars.test(fld.value)) { fld.style.background = 'Yellow'; error = "The email contains illegal characters. 100 Characters Max.\n"; } else { fld.style.background = 'White'; } return error; } function validatei_agree(fld) { var error = ""; if (document.getElementById("i_agree").checked=false) { error = "You must agree to the Terms and Conditions"; } return error; } </script> Link to comment https://forums.phpfreaks.com/topic/63933-function-not-working/ Share on other sites More sharing options...
akitchin Posted August 8, 2007 Share Posted August 8, 2007 try checking if you're actually getting the object referenced: function validatei_agree(fld) { var error = ""; alert("field passed is: "+fld+"\nfield reference is: "+document.getElementById("i_agree")+"\nchecked value is: "+document.getElementById("i_agree").checked); if (document.getElementById("i_agree").checked=false) { error = "You must agree to the Terms and Conditions"; } return error; } this should at least give you some insight into what's going on with your references and values. Link to comment https://forums.phpfreaks.com/topic/63933-function-not-working/#findComment-318662 Share on other sites More sharing options...
ballhogjoni Posted August 10, 2007 Author Share Posted August 10, 2007 I was finally able to implement what your were saying. The object is being passed but it is still not showing up, unless I keep the alert() in the script. Link to comment https://forums.phpfreaks.com/topic/63933-function-not-working/#findComment-319867 Share on other sites More sharing options...
gurroa Posted August 10, 2007 Share Posted August 10, 2007 Comparing is done via == sign not =. function validatei_agree(fld) { if (fld.checked == false) { // if (!fld.checked) delete fld; return "You must agree to the Terms and Conditions"; } delete fld; return ""; } Link to comment https://forums.phpfreaks.com/topic/63933-function-not-working/#findComment-320156 Share on other sites More sharing options...
ballhogjoni Posted August 10, 2007 Author Share Posted August 10, 2007 lol, I finally figured that out. I was speniding hours on this and I couldn't figure it out. I finally realized that I was assigning a value and not comparing the values. Link to comment https://forums.phpfreaks.com/topic/63933-function-not-working/#findComment-320557 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.