jQuery技术

jQuery.validationEngine.js学习(3)

字号+ 作者:H5之家 来源:H5之家 2017-01-02 15:02 我要评论( )

_getErrorMessage: function (form, field, rule, rules, i, options, originalValidationMethod) { rule_index = jQuery.inArray(rule, rules);(rule === custom || rule === funcCall ) { var custom_validation_

_getErrorMessage:function (form, field, rule, rules, i, options, originalValidationMethod) { rule_index = jQuery.inArray(rule, rules);(rule === "custom" || rule === "funcCall") { var custom_validation_type = rules[rule_index + 1]; rule = rule + "[" + custom_validation_type + "]"; (rules[rule_index]); } alteredRule = rule; var element_classes = (field.attr("data-validation-engine")) ? field.attr("data-validation-engine") : field.attr("class"); var element_classes_array = element_classes.split(" "); errorMsg; if (rule == "future" || rule == "past" || rule == "maxCheckbox" || rule == "minCheckbox") { errorMsg = originalValidationMethod(form, field, rules, i, options); } else { errorMsg = originalValidationMethod(field, rules, i, options);//执行回调函数,获得错误信息 } (errorMsg != undefined) { var custom_message = methods._getCustomErrorMessage($(field), element_classes_array, alteredRule, options); if (custom_message) errorMsg = custom_message; } return errorMsg; }

下面就是method._custom方法

_custom: function(field, rules, i, options) { var customRule = rules[i + 1]; var rule = options.allrules[customRule]; var fn; if(!rule) { //规则没有,则提示返回 alert("jqv:custom rule not found - "+customRule); return; } if(rule["regex"]) { (!ex) { alert("jqv:custom regex not found - "+customRule); return; } (!pattern.test(field.val())) return options.allrules[customRule].alertText;//匹配正则 } fn = rule["func"]; if (typeof(fn) !== "function") { alert("jqv:custom parameter 'function' is no function - "+customRule); return; } if (!fn(field, rules, i, options)) return options.allrules[customRule].alertText; } else { alert("jqv:custom type not allowed "+customRule); return; } }

其实看到这里,我们大致明白了,核心还是调用了rule['regex']的正则表达式。其实换了思维想一下,如果我们把email直接写在switch中,那其实会有几十种,甚至是几百种情况,那一起写在switch中明显是不明智的,这个custom相当于做了个简单的带头作用,将这些更细节的验证判断,统统挂上custom的名,再进行验证。那么这个_required其实很类似

_required: function(field, rules, i, options, condRequired) { "text": case "password": case "textarea": case "file": case "select-one": case "select-multiple": default: dv_placeholder = $.trim( field.attr("data-validation-placeholder") ); var placeholder = $.trim( field.attr("placeholder") ); if ( ( !field_val ) || ( dv_placeholder && field_val == dv_placeholder ) || ( placeholder && field_val == placeholder ) ) { return options.allrules[rules[i]].alertText;//返回不填的错误信息 } break; case "radio": case "checkbox": (condRequired) { if (!field.attr('checked')) { return options.allrules[rules[i]].alertTextCheckboxMultiple; } break; } form = field.closest("form, .validationEngineContainer"); var name = field.attr("name"); if (form.find("input[name='" + name + "']:checked").size() == 0) { if (form.find("input[name='" + name + "']:visible").size() == 1) return options.allrules[rules[i]].alertTextCheckboxe; else return options.allrules[rules[i]].alertTextCheckboxMultiple; } break; } }

这些方法进过验证之后,如果不满足情况,将会获得错误信息

在流程图的最后,整个插件的_showPrompt和_buildPrompt方法主要是生成页面提示

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • jQuery学习笔记--jqGrid的属性列表

    jQuery学习笔记--jqGrid的属性列表

    2017-01-02 14:04

  • JavaScript学习总结(四)

    JavaScript学习总结(四)

    2017-01-01 16:02

  • JQuery 学习:切换HTML元素的显示与隐藏

    JQuery 学习:切换HTML元素的显示与隐藏

    2016-12-30 15:01

  •  jQuery学习札记一

    jQuery学习札记一

    2016-12-30 15:00

网友点评
'