zoukankan      html  css  js  c++  java
  • jquery validate 自定义验证方法

    jQuery.validator.addMethod("stringCheck", function (value, element) {
        //return this.optional(element) || /^[u0391-uFFE5w]+$/.test(value);
        if ($("#txtProductName").val().length < 10) {
            return false;
        }
        else {
            return true;
        }
    }, "请输入适合的长度");
    
    //验证
    function validateFrom() {
        $("#form1").validate({
            rules: {
                txtProductName: {
                    required: true,
                    stringCheck: true
                },
                txtMarketPrice: {
                    required: true,
                    number: true
                },
                txtIntegral: {
                    required: true,
                    digits: true
                }
            },
            messages: {
                txtProductName: {
                    required: "请输入礼品名称"
                },
                txtMarketPrice: {
                    required: "请输入市场价"
                },
                txtIntegral: {
                    required: "请输入兑换积分"
                }
            }
            , success: function (label) {
                label.html("&nbsp;").attr("class", "success").siblings("label").remove();
            },
            errorPlacement: function (error, element) {
                $(element).next("span").find(".success").remove();
                error.appendTo(element.next("span"));
            }
        });
    
    }

    自定义验证信息

    jQuery.validator.addMethod("stringCheck", function (value, element) {
        //return this.optional(element) || /^[u0391-uFFE5w]+$/.test(value);
        if ($("#txtProductName").val().length < 10) {
            $.validator.messages["stringCheck"] = "错了";
            return false;
        }
        else {
            $.validator.messages["stringCheck"] = "对了";
            return true;
        }
    
    });
    
    //验证
    function validateFrom() {
        $("#form1").validate({
            rules: {
                txtProductName: {
                    required: true,
                    stringCheck: true
                },
                txtMarketPrice: {
                    required: true,
                    number: true
                },
                txtIntegral: {
                    required: true,
                    digits: true
                }
            },
            messages: {
                txtProductName: {
                    required: "请输入礼品名称"
                },
                txtMarketPrice: {
                    required: "请输入市场价"
                },
                txtIntegral: {
                    required: "请输入兑换积分"
                }
            }
            , success: function (label) {
                label.html("&nbsp;").attr("class", "success").siblings("label").remove();
            },
            errorPlacement: function (error, element) {
                $(element).next("span").find(".success").remove();
                error.appendTo(element.next("span"));
            }
        });
    
    }
  • 相关阅读:
    atom介绍
    举例介绍重构(译)
    java单双派机制理解
    AngularJS开发指南03:HTML编译器
    AngularJS开发指南02:引导程序
    AngularJS开发指南01:AngularJS简介
    1.angular之Hello World
    31天重构学习笔记(java版本)
    一个农夫的故事 分类: 其他 2015-01-24 16:44 104人阅读 评论(0) 收藏
    一个农夫的故事 分类: 其他 2015-01-24 16:44 103人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/hougelou/p/3572851.html
Copyright © 2011-2022 走看看