jQuery验证:如何忽略占位符文本(在默认/空白处产生错误) [英] Jquery Validate: How To Ignore Placeholder Text (Produce Error on Default/Blank)

查看:121
本文介绍了jQuery验证:如何忽略占位符文本(在默认/空白处产生错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让jquery验证忽略默认文本.
我检查默认文本的方式是检查element.value ==元素alt文本

I'd like to make jquery validate ignore the default text.
The way I have it check for default text is to check if the element.value == the elements alt text

这是我的代码,但是无论其空白,默认文本还是其他任何文本,它都将返回无效值:

Here's my code, but it returns invalid no matter if its blank, default text, or any other text:

$.validator.addMethod("notDefaultText", function(value) 
{
    if($(this).val()==$(this).attr('alt'))
              return false;

});

        rules: {
        Name: "required notDefaultText",
        Email: "required notDefaultText email"
    },

推荐答案

我遇到了同样的问题,并以这种方式解决了...

I had the same problem and solved it this way...

我检查默认文本的方式是检查值==元素占位符attr.

The way I have it check for default text is to check if the value == the elements placeholder attr.

$.validator.addMethod("notDefaultText", function (value, element) {
   if (value == $(element).attr('placeholder')) {
      return false;
   } else {
       return true;
     }
});

$("yourForm").validate({
        rules: {
            title: { required: true,
                notDefaultText: true
            },
            description: {
                required: true,
                notDefaultText: true,
                minlength: 2,
                maxlength: 400
            }
        },
        messages: {
            title: "Error text",
            description: {
                required: "Error text",
                notDefaultText: "Error textp",
                minlength: "Error text",
                maxlength: "Error text"
            }
        }

    });

这篇关于jQuery验证:如何忽略占位符文本(在默认/空白处产生错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆