refiking Posted June 9, 2010 Share Posted June 9, 2010 I am trying to create a function where it checks to see if the form variable companyfield has a value of "". If it does, it should show an error. Otherwise, it checks to see if companyfield has a value of Other. If it does, it then needs to check the formfield companyfieldother. If that has a value of "", it shows an error. Otherwise, it returns true. I'll send a copy of a password checking function (ConfirmRegPass) that goes with the script to see how I'm supposed to form it. Please help Remember, ConfirmRegPass works flawlessly with what I'm trying to accomplish. function ConfirmRegPass() { var frm = document.forms["register"]; if(frm.password.value != frm.password2.value) { sfm_show_error_msg("The Password and verified password don't match!",frm.password1); return false; } else { return true; } } function Companyfield() { var frm = document.forms["register"]; if(frm.companyfield.value == "") { sfm_show_error_msg("Please select a company field.",frm.companyfield); return false; } else { if(frm.companyfield.value == "Other") { if(frm.companyfieldother.value == ""){ sfm_show_error_msg("Please type your company field.",frm.companyfieldother); return false; } else{ return true; } } else { return true; } } } Quote Link to comment https://forums.phpfreaks.com/topic/204314-help-with-a-function/ Share on other sites More sharing options...
Psycho Posted June 10, 2010 Share Posted June 10, 2010 It's difficult to debug your code without seeing all the applicable code. Could be you are using the wrong field names or something. But, your code has more "logic" than it needs. For example you check passowrd function does not need an else. The IF will exit the function of validation fails, so you simply need to return true after the IF code. Assuming the code is referncing the correct fields and the values are what you expect, the code looks like it should work, but here is a rewrite that is more efficient and logical, IMO. function ConfirmRegPass() { var frm = document.forms["register"]; if(frm.password.value != frm.password2.value) { sfm_show_error_msg("The Password and verified password don't match!", frm.password1); return false; } return true; } function Companyfield() { var frm = document.forms["register"]; if(frm.companyfield.value == "") { sfm_show_error_msg("Please select a company field.",frm.companyfield); return false; } else if(frm.companyfield.value=="Other" && frm.companyfieldother.value == "") { sfm_show_error_msg("Please type your company field.", frm.companyfieldother); return false; } return true; } Quote Link to comment https://forums.phpfreaks.com/topic/204314-help-with-a-function/#findComment-1070454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.