当前位置: 代码迷 >> Web前端 >> jquery validator addMethod 步骤的使用
  详细解决方案

jquery validator addMethod 步骤的使用

热度:551   发布时间:2012-10-06 17:34:01.0
jquery validator addMethod 方法的使用

jQuery.validator.addMethod("isFigure",function(value,element,param){
??? ??? // 正则不能写错。
??? ??? var pattern = /^([0-9]*)+$/;
??? ??? return this.optional(element) || (!pattern.test(value));
??? },"店铺名称不能为纯数字。");
???
??? // 校验表单合法性
??? $("#form_openstore_org").validate(
??? ??? ??? {
??? ??? ??? ??? submitHandler : function(form) {
??? ??? ??? ??? ??? form.submit();
??? ??? ??? ??? },
??? ??? ??? ??? rules : { // 校验规则
??? ??? ??? ??? ??? storeName : {
??? ??? ??? ??? ??? ??? required : true,
??? ??? ??? ??? ??? ??? maxlength:40,
??? ??? ??? ??? ??? ??? minlength:4,
??? ??? ??? ??? ??? ??? isFigure:true,
??? ??? ??? ??? ??? ??? remote:
??? ??? ??? ??? ??? ??? {
??? ??? ??? ??? ??? ??? ??? url:"/app/usermanager/openstore/valid/reduplicate",//后台处理程序
??? ??? ??? ??? ??? ??? ??? type:"post",??????????????????????? //数据发送方式
??? ??? ??? ??? ??? ??? ??? dataType:"json", //接受数据格式?
??? ??? ??? ??? ??? ??? ??? data:{storeName:function(){return $("#storeName").val();}}?
??? ??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? }
??? ??? ??? ??? },
??? ??? ??? ??? messages : {
??? ??? ??? ??? ??? storeName : {
??? ??? ??? ??? ??? ??? required : "请输入您的店铺名称!",
??? ??? ??? ??? ??? ??? maxlength:"店铺名称太长!",
??? ??? ??? ??? ??? ??? minlength:"店铺名称不能少于4个字符!",
??? ??? ??? ??? ??? ??? isFigure:"店铺名称不能为纯数字。",
??? ??? ??? ??? ??? ??? remote:"店铺名称已注册!"
??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? }
??? ??? ??? ??? },
??? ??? ??? ??? errorPlacement : function(error, element) {
??? ??? ??? ??? ??? if (element.is(":input")||element.is(":select")) {
??? ??? ??? ??? ??? ??? // 得到当前标签下一标签是为label
??? ??? ??? ??? ??? ??? var placement = $(element.next("label"));
??? ??? ??? ??? ??? ??? placement.empty();
??? ??? ??? ??? ??? ??? placement.css("color", "#FF0000");
??? ??? ??? ??? ??? ??? error.prependTo(placement);
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? });

  相关解决方案