简体   繁体   中英

Form not triggering javascript onsubmit event

I have created a "signup" page on my site, but the javascript validation function does not seem to call when the "onsubmit" event is fired. I have already read the posts on this site regarding a similar issue, but their problem seems to be that they forgot to return a boolean value indicating to go ahead and perform the action. Note that this page is "included" in another page with the php function "include".

<script type="text/javascript">
    function validateForm()  {
        //alert();
        var display = document.getElementById('error');
        var usernameValue = document.getElementById('username').value;
        var passwordValue = document.getElementById('password').value;
        var rptPasswordValue = document.getElementById('rptPassword').value;
        var emailValue = document.getElementById('email').value;
        var aliasValue = document.getElementById('alias').value;

        if (usernameValue != '' && passwordValue != '' && rptPasswordValue != '' && emailValue != '' && aliasValue != '')  {
            if (passwordValue == rptPasswordValue)  {
                if (usernameValue.length > 32 || aliasValue.length > 32)  {
                    displayError("Username or password is too long (Both fields must contain 32 characters or less)");
                } else {
                    if (passwordValue.length > 32 || passwordValue.length < 4)  {
                        displayError("Password must be between 4 and 32 characters in length.");
                    } else {
                        if (emailValue.indexOf('@') == -1 || emailValue.length > 64 || emailValue.length < 6)  {
                            displayError("Please make sure you have entered a valid email. (Emails must be between 6 and 64 characters in length)");
                        } else {
                            if (aliasValue < 3 || aliasValue > 32)  {
                                displayError("Your alias must be between 3 and 32 characters in length.");
                            } else {
                                return true;
                            }
                        }
                    }
                }
            } else {
                displayError("Please make sure both passwords match.");
            }
        } else {
            displayError("Please make sure each field contains a value.");
        }

        return false;
    }

    function displayError(var msg)  {
        document.getElementById('error').innerHTML = msg;
    }
</script>
<h1>Sign Up</h1>
<p>All fields must be filled out in order for an account to be created.</p>
<form onsubmit="return validateForm();" action="register.php" method="post">
    <table>
        <tr>
            <td>
                <p class="txtLabel">Username:</p>
            </td>
            <td>
                <input type="text" class="defaultTxt" tabindex="0"  id="username" name='username'>
            </td>
            <td>
                <p class="txtLabel">Email Address:</p>
            </td>
            <td>
                <input type="text" class="defaultTxt" tabindex="1"  id="email" name='email'>
            </td>
        </tr>
        <tr>
            <td>
                <p class="txtLabel">Password:</p>
            </td>
            <td>
                <input type="password" class="defaultTxt" tabindex="2"  id="password" name='password'>
            </td>
        </tr>
        <tr>
            <td>
                <p class="txtLabel">Repeat Password:</p>
            </td>
            <td>
                <input type="password" class="defaultTxt" tabindex="2"  id="rptPassword" name='rptPassword'>
            </td>
        </tr>
        <tr>
            <td>
                <p class="txtLabel">Nickname/Alias:</p>
            </td>
            <td>
                <input type="text" class="defaultTxt" tabindex="4"  id="alias" name='alias'>
            </td>
        </tr>
    </table>
    <p><b id='error' style='color: red'></b></p><br />
    <input type="submit" value='' name='submit' class="submitBtn">
</form>

Note that in the validateForm function the alert was used to test if the function was called at all, it isn't.

Any ideas?

function displayError(var msg)  {
        document.getElementById('error').innerHTML = msg;
    }

Replace it with

function displayError(msg)  {
        document.getElementById('error').innerHTML = msg;
    }

In your java-script function you cant write like function displayError(var msg) .

Good luck.

Bug in your JavaScript here:

function displayError(var msg)  {
                      ^^^
     document.getElementById('error').innerHTML = msg;
}

You do not need the var keyword in a methods parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM