开发者

jQuery autoruns functions on change events when page is loaded

I have made a function that validates inputs with for different kind of strings. I am having serious issues with a few things. I call it on form submit and for each input on change. I have written my code below and then the calling of function. Thing is: when I call it on change and refresh the page, it automatically runs the validations on change when the page is loading and renders the errors.

var Val = {
    'string': function(ident, req, regexp, offset, limit) {
        var ele = $(document.getElementById(ident));
        Val.errors = false;

        if (!ele.val() && req == 1) {
            alert('blank');
            Val.errors = true;
            $("#" + ident + "Error").html("This field cannot be empty.");
            $("#" + ident + "Error").show("fast");
        }else if ((ele.val().length <= offset || ele.val().length > limit) && Val.errors == false) {
            alert('not long enough');
            Val.errors = true;
            $("#" + ident + "Error").html("This field must be between " + offset + " & " + limit + " charecters long");
            $("#" + ident + "Error").show("fast");
        }else if (regexp !== null) {
        switch (regexp) {
            case 'text':
                var regEx = /^([a-zA-Z]+)$/g;
                break;
            case 'number':
                var regEx = /^([0-9]+)$/g;
                break;
            case 'email':
                var regEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/g;
                break;
            case 'date':
                var regEx = /^([123]0|[012][1-9]|31)-(0[1-9]|1[012])-(19[0-9]{2}|2[0-9]{3})$/g;
                break;
            case 'alphanum':
                var regEx = /^([a-zA-Z0-9_\-\.]+)$/g;
                break;
            default:
                break;
        }
            if (!regEx.test(ele.val()) && Val.errors == false) {
                alert('not valid');
                Val.errors = true;
                $("#" + ident + "Error").html("This field is not valid");
                $("#" + ident + "Error").show("fast");
            }
        }
        if (!Val.errors){
            $("#" + ident + "Error").hide("fast");
        }
    },

    'send': function() {
        if (!Val.errors) {
            $('#form').submit();
        }
    }
}

Calling of the function:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form Validations</title>
<script type="text/javascript" language="JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" language="JavaScript" src="js/gcui.lsat.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#send').click(function(){
        checkEmail();
        checkUsername();
        Val.send(); 
    });

    $('#emailID').change(checkEmail());
    $('#username').change(checkUsername());


    function checkEmail(){
        Val.string('email', 1, 'username', 10, 100);
    }
    function checkUsername(){
        Val.string('username', 1, 'alphanum', 5, 15);
    }
});
</script>
</head>
<body>
    <form id="form" action=开发者_运维技巧"">
        <input type="text" id="email" name="email" title="Email" />
        <input type="text" id="username" name="username" title="Username" />
        <input type="button" id="send" value="Submit" />
    </form>

    Email: <div id="emailError"></div><br/>
    Username: <div id="usernameError"></div>
</body>
</html>


    $('#emailID').change(checkEmail());

Means "Run checkEmail and pass the return value to the change method"

You want:

    $('#emailID').change(checkEmail);

Meaning "Pass the checkEmail function to the change method"


In your $('#send').click(function(){ you do submit it two times. The first time when you do the validation and the second time when the button press is fired. Add the line Return False; just after the Val.send(); and you only get the forum to submit if there form are filled out correct

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜