Jquery验证与keyup和模糊在一起

时间:2015-07-03 02:11:35

标签: jquery

    $(document).ready(function() {
            //alert('hi');
            var form_name  = $('#reg_form_name');
            var reg_name = $('#reg_name');
            var reg_button = $('#register_submit');

我正在尝试使用keyup和blur blur验证reg_name

我收到以下错误:未捕获的TypeError:reg_name.on不是func

            reg_name.on('keyup blur', function (e){
            validateName(reg_name, reg_name_error, 'Full name');
            });

            form_name.submit(function(){
            // alert("form submited");

            if(validateName(reg_name, reg_name_error, 'firstname'))
            {
                return true;
            }
            else{
                return false;
            }

            }); //form submit

这是验证名称字段的功能

           function validateName(name, name_error, field_type){

                   var fname = name.val();

                   var regx_name = /^[a-z]{4,11}$/;

                   if(fname == ''){
                           name_error.html('Please enter ' + field_type);
                           return false;
                   }
                   if(regx_name.test(fname)){
                          // name_error.html('<img src="images/correct.png">');
                           name_error.html('ok');
                           return true;
                   }
                   else{
                           name_error.html('Only 4 to 10 characters allowed ');
                           return false;
                   }

           }// Validate Name function
     }); //doc ready

2 个答案:

答案 0 :(得分:1)

或者只是咬紧牙关然后循环它:

$.each(['keyup','blur']), function(){
reg_name.on(this, function (e){
            validateName(reg_name, reg_name_error, 'Full name');
            });
    form_name.submit(function(){
            // alert("form submited");

            if(validateName(reg_name, reg_name_error, 'firstname'))
            {
                return true;
            }
            else{
                return false;
            }

            });
}

答案 1 :(得分:1)

我知道了,我使用了绑定

reg_name.bind('keyup blur', function (e){
    validateName(reg_name, reg_name_error, 'Full name');
});