Last active
August 29, 2015 14:08
-
-
Save beiyuu/c6414ac2968435486b81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!(function($, _) { | |
var tmpl_error = '<div class="item-info text-red"><i class="iconfont icon-wrong"></i> {{msg}}</div>' | |
var tmpl_option = '<div class="item-info text-orange"><i class="iconfont icon-caution"></i> {{msg}}</div>' | |
function cancelZoom() { | |
var d = document, | |
viewport, | |
content, | |
maxScale = ',maximum-scale=', | |
maxScaleRegex = /,*maximum\-scale\=\d*\.*\d*/; | |
if(!this.addEventListener || !d.querySelector) { | |
return; | |
} | |
viewport = d.querySelector('meta[name="viewport"]'); | |
content = viewport.content; | |
function changeViewport(event) { | |
viewport.content = content + (event.type == 'blur' ? (content.match(maxScaleRegex, '') ? '' : maxScale + 10) : maxScale + 1); | |
} | |
this.addEventListener('focus', changeViewport, true); | |
this.addEventListener('blur', changeViewport, false); | |
} | |
$.fn.cancelZoom = function() { | |
return this.each(cancelZoom); | |
}; | |
$('input:text,select,textarea').cancelZoom(); | |
function errorHandler(is_error, ele, msg) { | |
var item = $(ele).parents('.item'); | |
var name = $(ele).parent().find('.input-unit').data('title') || '这'; | |
ele = $(ele); | |
$(ele.data('msg_ele')).remove() | |
if(is_error) { | |
var res = $(_.template(_.template(tmpl_error, {msg: msg}), {name: name})); | |
res.insertAfter(item); | |
ele.data('msg_ele', res); | |
} | |
}; | |
function optionHandler(ele, msg) { | |
var item = $(ele).parents('.item'); | |
ele = $(ele); | |
$(ele.data('msg_ele')).remove() | |
var res = $(_.template(tmpl_option, {msg: msg})); | |
res.insertAfter(item); | |
ele.data('msg_ele', res); | |
}; | |
function scrollToError(form) { | |
var error = $(form).find('.has-error'); | |
if(error.length) { | |
var top = $(error[0]).offset().top | |
$('body, html').animate({ scrollTop: top-50 }, 400, 'swing'); | |
} | |
} | |
var guihuaFormRules = { | |
adultAgeLimit: { | |
test: function(val) { | |
var val = parseInt(val, 10); | |
if(val > 99 || val < 18) { | |
return true; | |
} | |
return false; | |
}, | |
msg: '请正确填写成年人年龄' | |
} | |
} | |
$.extend({ | |
validate: { | |
errorHandler: errorHandler, | |
optionHandler: optionHandler, | |
scrollToError: scrollToError, | |
guihuaFormRules: guihuaFormRules | |
} | |
}); | |
})(jQuery, _); | |
//usage | |
var child_rules = { | |
credential: { | |
test: function(val) { | |
var phone_reg = /^\d{17}[x|X|\d]$/; | |
if(!val || phone_reg.test(val)) { | |
return false; | |
} | |
return true; | |
}, | |
msg: '请正确填写身份证号码' | |
}, | |
valid_date: { | |
test: function(val) { | |
if(!moment(val).isValid()){ | |
return true; | |
} | |
var now = moment(); | |
if(now.diff(moment(val), 'd') >= 0) { | |
return true; | |
} | |
return false; | |
}, | |
msg: '请选择正确的日期,不能早于今天' | |
} | |
} | |
var helper = $.validate; | |
var rules = $.extend(helper.guihuaFormRules, child_rules); | |
var config = { | |
errorHandler: helper.errorHandler, | |
optionHandler: helper.optionHandler, | |
failCallback: helper.scrollToError | |
}; | |
$('.js-child-form').validateForm(rules, config); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment