May 5, 200917 yr <script type="text/javascript"> <!-- function validate_form_1( form ) { if( form.elements['name'].value=="" ){alert("You must enter you're name");form.elements['name'].focus();return false;} return false; } --> </script> Above is some javascript to check if my name inputbox is empty or not, it works fine, { if ( form.elements['email'].value != ['emailconfirm'].value){alert("You're Emails are incorrect");form.elements['email'].focus(); return false; } return true; } Above this is the code to check the emails, this works on its own as well, ive tried multiple ways of adding them together, putting an else between them and taking the return true from the first statement, ive searched google, but i cant seem to find anything. Any help is really appreciated
May 5, 200917 yr I use a slightly different method to yours (I use "getElementById"), here's my code: <script type="text/javascript"> /* <![CDATA[ */ function checkform (mailform) { // Name if (document.getElementById('mailform').name.value == "") { alert( "Please enter your name." ); document.getElementById('mailform').name.focus(); return false; } // Contact Number if (document.getElementById('mailform').contact_number.value == "") { alert( "Please enter your contact number." ); document.getElementById('mailform').email.focus(); return false; } // Email Address if (document.getElementById('mailform').email.value == "") { alert( "Please enter your email address." ); document.getElementById('mailform').email.focus(); return false; } // Message if (document.getElementById('mailform').message.value == "") { alert( "Please enter your message." ); document.getElementById('mailform').message.focus(); return false; } return true; } /* ]]> */ </script> There's probably a better way of doing the above but my code works so as they say, if it ain't broke don't fix it.
May 5, 200917 yr Author Thanks for the reply, ill have a look and try it and out, fingers crossed Thanks again
May 5, 200917 yr Author Quick update, i got it working like a beauty Thanks Mate :D If i wanted to put this into a .js file how would i go about naming it? and if (document.getElementById('form_1').email.value != document.getElementById('form_1').confirmemail.value) { alert( "Please make sure your email address match." ); document.getElementById('form_1').email.focus(); return false; } i got that to check the emails match, but after i click ok on the alert box the form refreshes, it doesnt do this on the rest, have i changed something or is it because im doing a statement to see if they match? Thank you so much for the help so far, been ripping my hair out for hours over this.
May 5, 200917 yr I assume you could just copy and paste the function into a new file and save it as a .js file, then call it from your HTML page. My function doesn't check everything at once it just fails on the first thing that isn't filled in. When I get a chance I will probably rewrite it so it shows up all errors in one go.
Create an account or sign in to comment