ayok Posted February 24, 2008 Share Posted February 24, 2008 Hi, I've made a form that validate the name, email address, etc. So, if a visitor submit without filling their name, an alert box will appear and the visitor must klik ok and fill in the name field. It works properly on opera and ie, but not firefox. It always return true, and go to the next page. Why? This is the javascript code: <script type="text/javascript"> function valid(){ var check_lname= document.myform.lname.value var check_email= document.myform.email.value var check_check= document.myform.agree.checked if (check_lname==''){ alert('name required') myform.lname.focus() return false } if ((check_email=='') || (check_email.indexOf('@',0) == -1) || (check_email.indexOf('@',0) == -1)){ alert('valid email please') myform.email.focus() return false } if (check_check==''){ alert('You must agree.') myform.agree.focus() return false } return true } </script> <form method='post' name='myform' action='confirm.php' onSubmit='return valid()'> Could anyone help? Thanks ayok Quote Link to comment https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/ Share on other sites More sharing options...
paul2463 Posted February 24, 2008 Share Posted February 24, 2008 i think you may need a semi colon after the function call in the form tag <form method='post' name='myform' action='confirm.php' onSubmit='return valid();'> Quote Link to comment https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/#findComment-475146 Share on other sites More sharing options...
Psycho Posted February 24, 2008 Share Posted February 24, 2008 You might also need to change your variables as follows: var check_lname= document.forms['myform'].elements['lname'].value Quote Link to comment https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/#findComment-475152 Share on other sites More sharing options...
ayok Posted February 24, 2008 Author Share Posted February 24, 2008 Thanks for the replies guys, I've tried both, but still got this problem. The alert window is appeared but it goes to the next page after I click on ok. Quote Link to comment https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/#findComment-475182 Share on other sites More sharing options...
Psycho Posted February 24, 2008 Share Posted February 24, 2008 Can you post a link to the page? Quote Link to comment https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/#findComment-475350 Share on other sites More sharing options...
ayok Posted February 24, 2008 Author Share Posted February 24, 2008 Hi mjdamato, Contact form. Sorry, it's in dutch, I hope you can understand. thx, ayok Quote Link to comment https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/#findComment-475415 Share on other sites More sharing options...
emehrkay Posted February 24, 2008 Share Posted February 24, 2008 i think you are going about it the wrong way. I'll show you a cleaner way to do it. function validateForm(){ var form = document.getElementById('form_unique_id'); form.onsubmit = function(){ //do your checks in here "this" is the form element. so you can say this.elements['element name'].value; var ele1 = this.elements['ele1'].value; var error = false; if(ele1 == ''){ error = true; } if(error){ //do whatever return false; }else{ this.submit(); } }; } window.onload = validateForm; Quote Link to comment https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/#findComment-475426 Share on other sites More sharing options...
ayok Posted February 27, 2008 Author Share Posted February 27, 2008 Hi emehrkay, I've tried the codes, but it doesn't work either in firefox. Why firefox? Thanks, ayok Quote Link to comment https://forums.phpfreaks.com/topic/92721-validation-doesnt-work-on-firefox/#findComment-477842 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.