glennda Posted January 22, 2011 Posted January 22, 2011 I'm struggling to get my javascript form validation to work. I have read through a few tutorials and from what i can see i think i have it right. Although it still doesn't work! function validate_form ( ) { valid = true; if ( contact_form.staffname.value == " " ) { alert ( "Please fill in the 'Name' box." ); contact_form.staffname.focus(); valid = false; } if ( contact_form.email.value == " " ) { alert ( "Please fill in the 'Your Email' box." ); contact_form.email.focus(); valid = false; } if ( contact_form.week[0].checked == false ) { alert ( "Please choose either Week A or B" ); contact_form.week.focus(); valid = false; } if ( contact_form.absentallday[0].checked == false ) { alert ( "Please choose whether you are out all day or not." ); contact_form.absentallday.focus(); valid = false; } if ( contact_form.reason[0].checked == false ) && ( contact_form.reason[1].checked == false ) && ( contact_form.reason[2].checked == false ) && ( contact_form.reason[3].checked == false ) && ( contact_form.reason[4].checked == false ) && ( contact_form.reason[5].checked == false ) && ( contact_form.reason[6].checked == false ) ) { alert ( "Please Select a reason for absence." ); contact_form.reason.focus(); valid = false; } return valid; } here is my form tag </pre><form action="submitcover.php" method="post" name="contact_form" id="contact_form" onsubmit="return validate_form ( );"> < It doesn't work at all! it just submits the page regardless! i'm not quite sure what i have done wrong!
mac_shinobi Posted January 22, 2011 Posted January 22, 2011 Only thing I can see is the amount of brackets in your comparitive if statement at the end The IF Statement Half way down the above link they have more then one set of brackets around each comparitive section ie if ([b](red == blue) && (red == green)[/b]) { match = 'equal'; } else { match = 'unequal'; }
glennda Posted January 22, 2011 Author Posted January 22, 2011 okay i've added the extra brackets in (the example i had didn't show them!) it prompts for validation but still sends the email without letting you correct!?
mac_shinobi Posted January 22, 2011 Posted January 22, 2011 (edited) does the return valid; code set valid to true in which case it thinks that everything is valid and it then sends the email using your original code without the correct amount of brackets if ( contact_form.reason[0].checked == false ) && ( contact_form.reason[1].checked == false ) && ( contact_form.reason[2].checked == false ) && ( contact_form.reason[3].checked == false ) && ( contact_form.reason[4].checked == false ) && ( contact_form.reason[5].checked == false ) && ( contact_form.reason[6].checked == false ) ) { alert ( "Please Select a reason for absence." ); contact_form.reason.focus(); [b]valid = false;[/b] } [b] return valid;[/b] } In the if statement you set valid to false but afterwards it is set to return valid; Just curious if you comment out the return valid; Maybe its just me but I would of made it set the valid flag to true when the data entered is correct and then check on the last if statement to ensure all of them are valid ie true before sending the email Maybe Im just thinking about this in a backwards way Edited January 22, 2011 by mac_shinobi
glennda Posted January 22, 2011 Author Posted January 22, 2011 but shouldn't return valid just return the value of valid? its set to true at the top and if they statements are correct it sets it to false? I will give it a try tho
mac_shinobi Posted January 22, 2011 Posted January 22, 2011 Setting the valid flag to false to start with and setting them to true if the data requirement for said field is correct fair enough ref the return valid is there any code / function to send the email as I can't see which bit(s) of code are calling to send the email Am wondering if its the php code as when you submit its executing the php code
glennda Posted January 22, 2011 Author Posted January 22, 2011 The PHP script just submits but I presumed if the valid flag was false then it shouldn't action!
mac_shinobi Posted January 22, 2011 Posted January 22, 2011 Depends on what's submitting the email ie php or JavaScript and also if the code is checking the valid flag to see if each of thm is set to true before sending the data from the said form and if there is any invalid data ie any of the flags are false to not do anything 1
glennda Posted January 22, 2011 Author Posted January 22, 2011 I have changed it so that if the javascript fails then it doesn't send the form. I put in document.formname.submit() return; } now it works Cheers for that prompted me not to give up trying!
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