jQuery验证器addMethod总是返回false

时间:2013-07-12 09:37:39

标签: javascript jquery validation

我对jQuery验证http://validation.bassistance.de/相当新,并尝试使用addMethod创建自定义事件。我编写了看似正确但看起来不正确的内容,因为如果事件为真,则应该隐藏#email而不显示任何消息。

正在发生的事情是,如果事件为真或假,则消息“请输入一个值进行测试!'永远被解雇了。我显然犯了一个史诗般的错误,但似乎无法找到纠正它的方法。如果有人能指出我的错误,我将不胜感激。我只发布了相关的代码。感谢

小提琴代码:http://jsfiddle.net/DQjTc/

更新:只是更新。我注意到如果在验证后按下提交按钮,则输入正确的消息而虚假消息不输入。这就是我需要的。我们从哪里开始。

addMethod代码

    $(function() {

    $.validator.addMethod( 'emailHide', function(value, element) 
    {

        if (element.value === '')

            {

                return false;
            }


                //return true;
                $('#email').hide();
                $('#emaillbl').html('Email address approved');


        },'Please enter a value for testing!'


    );
});

验证规则代码

    $(function () {

    $.validator.setDefaults(
    {
        errorClass: 'form_error_frmreport',
        errorElement: 'div'
    });

    $("#frmreport").validate(
    {
        rules:
        {

            email:
            {
                //required: true,
                email: true,
                emailHide: true
            },
            position:
            {
                required: true
            },
            feedback:
            {
                required: true
            }
        },
        messages:
        {


            email:
            {
                //required: "<br />* required: You must enter a valid email address"
            },
            position:
            {
                required: "<br />* required: Please state your position"
            },
            feedback:
            {
                required: "<br />* required: Please enter as much information regarding the exact nature of the problem"
            }
        },

        submitHandler: function()   {
                if ($("#frmreport").valid() === true)  { 
                var data = $("#frmreport").serialize();
                $.post('/sample/admin/frm10010.php', data, function(msg) {

               var messageOutput = '';
                for (var i = 0; i<msg.length; i++){
                    messageOutput += msg[i].box+'  ';     
                }
                $("#confirm_department").hide();


               var $dialog = $('<div id="dialog"></div>')
               .html('Your report was successfully submitted and a representative will respond to you shortly.<br /><br />Thank you.');
               $dialog.dialog({
               autoOpen: true,
               modal: true,
               title: 'Report submission successfull',
               width: 400,
               height: 200,
               draggable: false,
               resizable: false,
               buttons: {
               Close: function() {
               $( this ).dialog( "close" );
               }
               }
               });
                $("#frmreport").get(0).reset();
                }, 'json');

         } else

         { 
           return; 
         }
        },
        success:  function()   {

                //validator.resetForm();
                //$.html("You have entered a box");
                //$("#BA_boxform").get(0).reset();
        }   

    });
});

2 个答案:

答案 0 :(得分:1)

该值可能包含空格,因此您应该尝试:

if (element.value.trim() == '')

并非所有浏览器都支持trim,因此您应将其放在Javascript文件的顶部。

if(!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g,'');
  };
}

答案 1 :(得分:1)

不确定但试试这个 将 if (element.value ==='') **替换为** if (value ==='')