Javascript form validation resets the form in firefox.

Discussion in 'JavaScript' started by Talker, Mar 7, 2008.

  1. #1
    Hi,

    Has anyone come across this validation bug?

    
    <script language="Javascript" type="text/javascript">
    
    <!--
    function validate() {
        //First we check for errors.
        var flag = 0;
        var errors = "";
        //If a name is not specified, show an error
        if (document.form1.fullname.value=="") {    
            flag++;
            errors = errors + "Full Name not specified\n";
        }
    
        //If a Company is not specified, show an error
        if (document.form1.company.value=="") {    
            flag++;
            errors = errors + "Company not specified\n";
        }
        
        
        //Check if email is a valid E-mail id.
        var id = document.form1.emailaddress.value;
        //The regular expression for a valid e-mail address
        var re = /^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/gi; 
        if(!id.match(re)) { //If it is NOT a match
            flag++;
            errors = errors + "E-Mail invalid\n";
        }
        //Dispay an error message if there was any error
        if(flag > 0) {
            alert("Errors were found in these elements:\n\n"+
                errors+"\nPlease re-enter these elements.");
    
            
            return false; //Exit the function if error is there
            
        }
    
    
        return true;
        }
    //-->
    </script>
    
    Code (markup):
    This code seems to work fine for Internet Explorer. When a text field is left empty an alert is fired and then it retains all the other entered form values. But the same code does not work nicely in firefox. After the alert, ALL the text fields are emptied, in other words the form is reseted. Its not nice for a visitor to re-enter all the information from scratch.

    Does anyone have a workaround for this bug?
     
    Talker, Mar 7, 2008 IP
  2. Talker

    Talker Notable Member

    Messages:
    2,795
    Likes Received:
    108
    Best Answers:
    0
    Trophy Points:
    210
    #2
    Alright everyone. i seem to have found the issue.
    I had to remove the RESET button off the form and now firefox does not reset the form...

    Seems a bit ridiculous but i will have to code a button to reset the form fields now.
    Well at least this post might help others..

    Talker
     
    Talker, Mar 7, 2008 IP