jQuery验证和占位符冲突

时间:2010-12-10 15:02:20

标签: javascript jquery html validation placeholder

我正在使用jQuery Validation插件验证我网站上的表单。

http://docs.jquery.com/Plugins/Validation

我还使用以下代码为不支持HTML5 placeholder=""属性的浏览器提供占位符支持。

// To detect native support for the HTML5 placeholder attribute
var fakeInput = document.createElement("input"),
    placeHolderSupport = ("placeholder" in fakeInput);

// Applies placeholder attribute behavior in web browsers that don't support it
if (!placeHolderSupport) {

    $('[placeholder]').focus(function() {
        var input = $(this);
        if (input.val() == input.attr('placeholder')) {
            input.val('');
            input.removeClass('placeholder');
        }
    }).blur(function() {
        var input = $(this);
        if (input.val() == '') {
            input.addClass('placeholder');
            input.val(input.attr('placeholder'));
        }
    }).blur().parents('form').submit(function() {
        $(this).find('[placeholder]').each(function() { //line 20
            var input = $(this);
            if (input.val() == input.attr('placeholder')) {
                input.val('');
            }
        });
    });
}

当我提交表单时,会发生以下情况:

在支持placeholder属性的浏览器中,validate()函数会触发,并且所有内容都按预期运行。

在不支持placeholder属性的浏览器中,第20-25行清除所有“占位符”,然后触发validate()函数。如果没有错误,页面提交并且一切都按预期工作。

在不受支持的浏览器中,如果出现错误,相应的字段会像往常一样应用class="error" - 但占位符文本在特定事件发生blur()事件之前不会再回来领域。这会将这些字段留空 - 因为没有标签(只有placeholder属性),用户只能猜测每个空字段应包含的内容,直到blur()事件发生。

不受支持的浏览器存在的另一个问题是,由于占位符修复修改了value属性以显示占位符,因此标​​记为必需的字段会在失败时通过验证。

似乎没有简单的方法将Validation插件与占位符支持代码一起使用。

我希望修改占位符支持代码或将submitHandler: {}函数作为参数添加到validate()函数中,以使其在不受支持的浏览器中运行。

4 个答案:

答案 0 :(得分:7)

我遇到了类似的问题。你有没有上班的?我很乐意比较笔记。

FWIW,这就是我所做的:

jsfiddle demo here

将输入占位符添加到jQuery支持对象:

$.support.placeholder = (function() {
    var i = document.createElement( 'input' );
    return 'placeholder' in i;
})();

占位符链:

$('input')
    .addClass('hint')
    .val( function() {
        if ( !$.support.placeholder ) {
            return $(this).attr('placeholder');
        }
    })
    .bind({
        focus: function() {
            var $this = $(this);
            $this.removeClass('hint');
            if ( $this.val() === $this.attr('placeholder') ) {
                $this.val('');
            }
        },
        blur: function() {
            var $this = $(this),

                // Trim whitespace if only space characters are entered,
                // which breaks the placeholders.
                val = $.trim( $this.val() ),
                ph = $this.attr('placeholder');

            if ( val === ph || val === '' ) {
                $this.addClass('hint').val('');
                if ( !$.support.placeholder ) {
                    $this.val(ph);
                }
            }
        }
    });

添加新的验证规则

addMethod docs

$.validator.addMethod('notPlaceholder', function(val, el) {
    return this.optional(el) || ( val !== $(el).attr('placeholder') );
}, $.validator.messages.required);

在验证规则对象中包含新方法

$('form').validate({
    rules: {
        name: {
            required: true,
            notPlaceholder: true
        },
        email: {
            required: true,
            notPlaceholder: true,
            email: true
        }
    }
});

答案 1 :(得分:2)

我认为将这个添加到jquery.validate.js到required函数(第900行)是最好的:

required: function(value, element, param) {

        // Our change
        if (element.value == element.defaultValue) {
            return false;
        }
        // End our change

答案 2 :(得分:1)

你可以通过将它绑定到提交函数(通过jQuery验证或手动)来解决这个问题

 if(element.val() == text){
      element.val('');
 }

答案 3 :(得分:1)

Placeholder插件更新解决了我的问题:)

相关问题