首页 新闻 会员 周边

请教了jquery.validate.js只能表单form验证吗,可以只验证几个控件吗,有代码的更好3q

0
悬赏园豆:5 [已解决问题] 解决于 2014-07-05 17:47

请教大虾jquery.validate.js只能表单form验证吗,可以只验证几个控件吗,有代码的更好3q,

豆豆少了点,更加感谢回答的人

觉信的主页 觉信 | 初学一级 | 园豆:66
提问于:2013-12-04 09:58
< >
分享
最佳答案
0

验证的 规则需要自己写。验证的元素是自己定的 。

 

以下 是一个完整的 注册验证  C#  异步验证

  $(document).ready(function ()
        {
            // 页面加载时调用
            $(function ()
            {
                $('#registerform').validate({
                    debug: true,

                    rules: {

                        txtEmail: {
                            required: true,
                            email: true,
                            maxlength: 40,
                            remote: {
                                url: "../common/registandlogin.ashx?cmd=email",     //后台处理程序
                                type: "get",               //数据发送方式
                                dataType: "json",           //接受数据格式
                                data: {//要传递的数据
                                    emailcode: function ()
                                    {
                                        return $("#txtEmail").val();
                                    }
                                }
                            }
                        },
                        txtUserPwd: {
                            required: true,
                            maxlength: 18,
                            minlength: 6
                        },
                        txtRUserPwd: {
                            required: true,
                            equalTo: "#txtUserPwd"
                        },

                        txtPhone: {
                            required: true,
                            minlength: 7,
                            maxlength: 13,
                            isMobile: true
                        },
                        txtCheck: "required",
                        captcha: {
                            required: true,
                            remote: {
                                url: "../common/registandlogin.ashx?cmd=captcha",     //后台处理程序
                                type: "get",               //数据发送方式
                                dataType: "json",           //接受数据格式
                                data: {//要传递的数据
                                    vcode: function ()
                                    {
                                        return $("#txtcaptcha").val();
                                    }
                                }
                            }
                        }
                    },
                    messages: {

                        txtEmail: {
                            required: "邮箱不能为空!",
                            email: "邮箱格式不正确!",
                            maxlength: "最长40个字符!",
                            remote: "很遗憾,您输入的邮箱已被使用!"

                        },
                        txtUserPwd: {
                            required: "密码不能为空!",
                            maxlength: "最长为18个字符!",
                            minlength: "最短为6个字符!"

                        },
                        txtRUserPwd: {
                            required: "密码不能为空!",
                            equalTo: "两次输入密码不相同!"
                        },
                        txtPhone: {
                            required: "电话不能为空!",
                            minlength: "最少7个数字!",
                            maxlength: "最长13个数字!",
                            isPhone: "联系电话格式格式不正确!"
                        },
                        txtCheck: {
                            required: "请阅读用户协议!"
                        },
                        captcha: {
                            required: "请输入验证码!",
                            remote: "验证码输入错误!"
                        }
                    },
                    validClass: "sucess",
                    errorClass: "error",
                    errorElement: "p",
                    focusCleanup: true, //被验证的元素获得焦点时移除错误信息 
                    errorPlacement: function (error, element)
                    {

                        error.appendTo(element.parent());
                    },
                    submitHandler: function (form)
                    {
                        var formData = $(form).serialize();
                        $.ajax({
                            cache: false,
                            type: "POST",
                            url: "../common/registandlogin.ashx?cmd=add&tk=" + (new Date()).getTime(),
                            data: formData,
                            error: function (request)
                            {
                                alert('链接失败.');
                            },
                            success: function (data)
                            {

                                if (data == "true")
                                {

                                    location.href = 'memberlogin.aspx?email=' + $("#txtEmail").val();
                                } else
                                {
                                    alert('注册失败!');
                                }
                            }
                        });
                    },
                    success: function (p)
                    {
                        p.html(" ").addClass("sucess");
                    }

                });
                //所有使用“.registerText”样式的文本框加上效果,获得焦点文本框变成淡黄色 
                $(".registerText").focus(function ()
                {
                    $(this).css("background-color", "#FFFFCC").blur(function ()
                    {
                        $(this).css("background-color", "#FBFBFB");
                    });
                });
                $(".registerText_yzm").focus(function ()
                {
                    $(this).css("background-color", "#FFFFCC").blur(function ()
                    {
                        $(this).css("background-color", "#FBFBFB");
                    });
                });

            });
        });

收获园豆:3
Постой! | 小虾三级 |园豆:1084 | 2013-12-04 13:37

 txtEmail 以及类似 的是 要验证元素的  name 属性值

Постой! | 园豆:1084 (小虾三级) | 2013-12-04 13:40
其他回答(4)
0

自己一个个写验证就可以了

拾梦小侠ด้้้ | 园豆:713 (小虾三级) | 2013-12-04 12:35
0

一般要验证的,都是要提交到后台的,在form表单中,只验证一部分就可以了,

第一种:

$("#myform").validate({
        rules: {
            //这里不进行校验(校验规则),当然下面也不写校验不通过的提示信息了            
            agree: "required"
        },
        messages: {            
            agree: "Please accept our policy"
        }

第二种就是获取表单只验证表单内的某一个元素

var validator = $( "#myform" ).validate();

validator.element( "#myselect" );

其实查看文档,是学习的好方法,这是文档地址http://jqueryvalidation.org/documentation/

收获园豆:2
秋壶冰月 | 园豆:5903 (大侠五级) | 2013-12-04 12:57
0

不想验证的控件上不加验证规则不就行了吗

junjieok | 园豆:779 (小虾三级) | 2013-12-04 13:43
0

应该是要个表单的

chenping2008 | 园豆:9836 (大侠五级) | 2013-12-04 13:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册