开发者

Jquery Validate: How To Ignore Placeholder Text (Produce Error on Default/Blank)

开发者 https://www.devze.com 2023-02-08 11:07 出处:网络
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

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;

});

        ru开发者_JAVA百科les: {
        Name: "required notDefaultText",
        Email: "required notDefaultText email"
    },


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

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"
            }
        }

    });


HTML5 has support for placeholders as part of markup. You can use jQuery to handle other browsers.

A well presented way to do this is presented at http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html


You are probably using the alt on a element that does not take that attribute alt is for images and etc not input fields! Therefore the browser will not accept it.

Try an attribute like: "data-default" or anything with 'data-*' to store these values.

sometimes I find some browsers to read/manipulate certain attributes acting with unexpected behavior.

0

精彩评论

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