jquery.validator.js 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. ;(function(factory) {
  2. typeof module === 'object' && module.exports ? module.exports = factory( require( 'jquery' ) ) :
  3. typeof define === 'function' && define.amd ? define(['jquery'], factory) :
  4. factory(jQuery);
  5. }(function($, undefined) {
  6. 'use strict';
  7. var NS = 'validator',
  8. CLS_NS = '.' + NS,
  9. CLS_NS_RULE = '.rule',
  10. CLS_NS_FIELD = '.field',
  11. CLS_NS_FORM = '.form',
  12. CLS_WRAPPER = 'nice-' + NS,
  13. CLS_MSG_BOX = 'msg-box',
  14. ARIA_INVALID = 'aria-invalid',
  15. DATA_RULE = 'data-rule',
  16. DATA_MSG = 'data-msg',
  17. DATA_TIP = 'data-tip',
  18. DATA_OK = 'data-ok',
  19. DATA_TIMELY = 'data-timely',
  20. DATA_TARGET = 'data-target',
  21. DATA_DISPLAY = 'data-display',
  22. DATA_MUST = 'data-must',
  23. NOVALIDATE = 'novalidate',
  24. INPUT_SELECTOR = ':verifiable',
  25. rRules = /(&)?(!)?\b(\w+)(?:\[\s*(.*?\]?)\s*\]|\(\s*(.*?\)?)\s*\))?\s*(;|\|)?/g,
  26. rRule = /(\w+)(?:\[\s*(.*?\]?)\s*\]|\(\s*(.*?\)?)\s*\))?/,
  27. rDisplay = /(?:([^:;\(\[]*):)?(.*)/,
  28. rDoubleBytes = /[^\x00-\xff]/g,
  29. rPos = /top|right|bottom|left/,
  30. rAjaxType = /(?:(cors|jsonp):)?(?:(post|get):)?(.+)/i,
  31. rUnsafe = /[<>'"`\\]|&#x?\d+[A-F]?;?|%3[A-F]/gmi,
  32. noop = $.noop,
  33. proxy = $.proxy,
  34. trim = $.trim,
  35. isFunction = $.isFunction,
  36. isString = function(s) {
  37. return typeof s === 'string';
  38. },
  39. isObject = function(o) {
  40. return o && Object.prototype.toString.call(o) === '[object Object]';
  41. },
  42. isIE = document.documentMode || +(navigator.userAgent.match(/MSIE (\d+)/) && RegExp.$1),
  43. attr = function(el, key, value) {
  44. if (!el || !el.tagName) return null;
  45. if (value !== undefined) {
  46. if (value === null) el.removeAttribute(key);
  47. else el.setAttribute(key, '' + value);
  48. } else {
  49. return el.getAttribute(key);
  50. }
  51. },
  52. novalidateonce,
  53. preinitialized = {},
  54. defaults = {
  55. debug: 0,
  56. theme: 'default',
  57. ignore: '',
  58. focusInvalid: true,
  59. focusCleanup: false,
  60. stopOnError: false,
  61. beforeSubmit: null,
  62. valid: null,
  63. invalid: null,
  64. validation: null,
  65. formClass: 'n-default',
  66. validClass: 'n-valid',
  67. invalidClass: 'n-invalid',
  68. bindClassTo: null,
  69. remoteDataType: 'cors'
  70. },
  71. fieldDefaults = {
  72. timely: 1,
  73. display: null,
  74. target: null,
  75. ignoreBlank: false,
  76. showOk: true,
  77. // Translate ajax response to validation result
  78. dataFilter: function (data) {
  79. if ( isString(data) || ( isObject(data) && ('error' in data || 'ok' in data) ) ) {
  80. return data;
  81. }
  82. },
  83. msgMaker: function(opt) {
  84. var html;
  85. html = '<span role="alert" class="msg-wrap n-'+ opt.type + '">' + opt.arrow;
  86. if (opt.result) {
  87. $.each(opt.result, function(i, obj){
  88. html += '<span class="n-'+ obj.type +'">' + opt.icon + '<span class="n-msg">' + obj.msg + '</span></span>';
  89. });
  90. } else {
  91. html += opt.icon + '<span class="n-msg">' + opt.msg + '</span>';
  92. }
  93. html += '</span>';
  94. return html;
  95. },
  96. msgWrapper: 'span',
  97. msgArrow: '',
  98. msgIcon: '<span class="n-icon"></span>',
  99. msgClass: 'n-right',
  100. msgStyle: '',
  101. msgShow: null,
  102. msgHide: null
  103. },
  104. themes = {};
  105. /** jQuery Plugin
  106. * @param {Object} options
  107. debug {Boolean} 0 Whether to enable debug mode
  108. timely {Number} 1 Whether to enable timely validation
  109. theme {String} 'default' Theme name
  110. stopOnError {Boolean} false Whether to stop validate when found an error input
  111. focusCleanup {Boolean} false Whether to clean up the field message when focus the field
  112. focusInvalid {Boolean} true Whether to focus the field that is invalid
  113. ignoreBlank {Boolean} false When the field has no value, whether to ignore validation
  114. ignore {jqSelector} '' Ignored fields (Using jQuery selector)
  115. beforeSubmit {Function} Do something before submit form
  116. dataFilter {Function} Convert ajax results
  117. valid {Function} Triggered when the form is valid
  118. invalid {Function} Triggered when the form is invalid
  119. validClass {String} 'n-valid' Add this class name to a valid field
  120. invalidClass {String} 'n-invalid' Add this class name to a invalid field
  121. bindClassTo {jqSelector} ':verifiable' Which element should the className binding to
  122. display {Function} Callback function to get dynamic display
  123. target {Function} Callback function to get dynamic target
  124. msgShow {Function} Trigger this callback when show message
  125. msgHide {Function} Trigger this callback when hide message
  126. msgWrapper {String} 'span' Message wrapper tag name
  127. msgMaker {Function} Callback function to make message HTML
  128. msgArrow {String} Message arrow template
  129. msgIcon {String} Message icon template
  130. msgStyle {String} Custom message css style
  131. msgClass {String} Additional added to the message class names
  132. formClass {String} Additional added to the form class names
  133. messages {Object} Custom messages for the current instance
  134. rules {Object} Custom rules for the current instance
  135. fields {Object} Field validation configuration
  136. {String} key name|#id
  137. {String|Object} value Rule string or an object which can pass more arguments
  138. fields[key][rule] {String} Rule string
  139. fields[key][display] {String|Function}
  140. fields[key][tip] {String} Custom tip message
  141. fields[key][ok] {String} Custom success message
  142. fields[key][msg] {Object} Custom error message
  143. fields[key][msgStyle] {String} Custom message style
  144. fields[key][msgClass] {String} A className which added to message placeholder element
  145. fields[key][msgWrapper] {String} Tag name of the message placeholder element
  146. fields[key][msgMaker] {Function} A function to custom message HTML
  147. fields[key][dataFilter] {Function} A function to convert ajax results
  148. fields[key][valid] {Function} A function triggered when field is valid
  149. fields[key][invalid] {Function} A function triggered when field is invalid
  150. fields[key][must] {Boolean} If set true, we always check the field even has remote checking
  151. fields[key][timely] {Boolean} Whether to enable timely validation
  152. fields[key][target] {jqSelector} Define placement of a message
  153. */
  154. $.fn.validator = function(options) {
  155. var that = this,
  156. args = arguments;
  157. if (that.is(INPUT_SELECTOR)) return that;
  158. if (!that.is('form')) that = this.find('form');
  159. if (!that.length) that = this;
  160. that.each(function() {
  161. var instance = $(this).data(NS);
  162. if (instance) {
  163. if ( isString(options) ) {
  164. if ( options.charAt(0) === '_' ) return;
  165. instance[options].apply(instance, [].slice.call(args, 1));
  166. }
  167. else if (options) {
  168. instance._reset(true);
  169. instance._init(this, options);
  170. }
  171. } else {
  172. new Validator(this, options);
  173. }
  174. });
  175. return this;
  176. };
  177. // Validate a field, or an area
  178. $.fn.isValid = function(callback, hideMsg) {
  179. var me = _getInstance(this[0]),
  180. hasCallback = isFunction(callback),
  181. ret, opt;
  182. if (!me) return true;
  183. if (!hasCallback && hideMsg === undefined) hideMsg = callback;
  184. me.checkOnly = !!hideMsg;
  185. opt = me.options;
  186. ret = me._multiValidate(
  187. this.is(INPUT_SELECTOR) ? this : this.find(INPUT_SELECTOR),
  188. function(isValid){
  189. if (!isValid && opt.focusInvalid && !me.checkOnly) {
  190. // navigate to the error element
  191. me.$el.find('[' + ARIA_INVALID + ']:first').focus();
  192. }
  193. if (hasCallback) {
  194. if (callback.length) {
  195. callback(isValid);
  196. } else if (isValid) {
  197. callback();
  198. }
  199. }
  200. me.checkOnly = false;
  201. }
  202. );
  203. // If you pass a callback, we maintain the jQuery object chain
  204. return hasCallback ? this : ret;
  205. };
  206. $.extend($.expr.pseudos || $.expr[':'], {
  207. // A faster selector than ":input:not(:submit,:button,:reset,:image,:disabled,[contenteditable])"
  208. verifiable: function(elem) {
  209. var name = elem.nodeName.toLowerCase();
  210. return ( name === 'input' && !({submit: 1, button: 1, reset: 1, image: 1})[elem.type] ||
  211. name === 'select' ||
  212. name === 'textarea' ||
  213. elem.contentEditable === 'true'
  214. ) && !elem.disabled;
  215. },
  216. // any value, but not only whitespace
  217. filled: function(elem) {
  218. return !!trim($(elem).val());
  219. }
  220. });
  221. /**
  222. * Creates a new Validator
  223. *
  224. * @class
  225. * @param {Element} element - form element
  226. * @param {Object} options - options for validator
  227. */
  228. function Validator(element, options) {
  229. var me = this;
  230. if ( !(me instanceof Validator) ) {
  231. return new Validator(element, options);
  232. }
  233. if (Validator.pending) {
  234. $(window).on('validatorready', init);
  235. } else {
  236. init();
  237. }
  238. function init() {
  239. me.$el = $(element);
  240. if (me.$el.length) {
  241. me._init(me.$el[0], options);
  242. }
  243. else if (isString(element)) {
  244. preinitialized[element] = options;
  245. }
  246. }
  247. }
  248. Validator.prototype = {
  249. _init: function(element, options) {
  250. var me = this,
  251. opt, themeOpt, dataOpt;
  252. // Initialization options
  253. if ( isFunction(options) ) {
  254. options = {
  255. valid: options
  256. };
  257. }
  258. options = me._opt = options || {};
  259. dataOpt = attr(element, 'data-'+ NS +'-option');
  260. dataOpt = me._dataOpt = dataOpt && dataOpt.charAt(0) === '{' ? (new Function('return ' + dataOpt))() : {};
  261. themeOpt = me._themeOpt = themes[ options.theme || dataOpt.theme || defaults.theme ];
  262. opt = me.options = $.extend({}, defaults, fieldDefaults, themeOpt, me.options, options, dataOpt);
  263. me.rules = new Rules(opt.rules, true);
  264. me.messages = new Messages(opt.messages, true);
  265. me.Field = _createFieldFactory(me);
  266. me.elements = me.elements || {};
  267. me.deferred = {};
  268. me.errors = {};
  269. me.fields = {};
  270. // Initialization fields
  271. me._initFields(opt.fields);
  272. // Initialization events and make a cache
  273. if ( !me.$el.data(NS) ) {
  274. me.$el.data(NS, me).addClass(CLS_WRAPPER +' '+ opt.formClass)
  275. .on('form-submit-validate', function(e, a, $form, opts, veto) {
  276. me.vetoed = veto.veto = !me.isValid;
  277. me.ajaxFormOptions = opts;
  278. })
  279. .on('submit'+ CLS_NS +' validate'+ CLS_NS, proxy(me, '_submit'))
  280. .on('reset'+ CLS_NS, proxy(me, '_reset'))
  281. .on('showmsg'+ CLS_NS, proxy(me, '_showmsg'))
  282. .on('hidemsg'+ CLS_NS, proxy(me, '_hidemsg'))
  283. .on('focusin'+ CLS_NS + ' click'+ CLS_NS, INPUT_SELECTOR, proxy(me, '_focusin'))
  284. .on('focusout'+ CLS_NS +' validate'+ CLS_NS, INPUT_SELECTOR, proxy(me, '_focusout'))
  285. .on('keyup'+ CLS_NS +' input'+ CLS_NS + ' compositionstart compositionend', INPUT_SELECTOR, proxy(me, '_focusout'))
  286. .on('click'+ CLS_NS, ':radio,:checkbox', 'click', proxy(me, '_focusout'))
  287. .on('change'+ CLS_NS, 'select,input[type="file"]', 'change', proxy(me, '_focusout'));
  288. // cache the novalidate attribute value
  289. me._NOVALIDATE = attr(element, NOVALIDATE);
  290. // Initialization is complete, stop off default HTML5 form validation
  291. // If use "jQuery.attr('novalidate')" in IE7 will complain: "SCRIPT3: Member not found."
  292. attr(element, NOVALIDATE, NOVALIDATE);
  293. }
  294. // Display all messages in target container
  295. if ( isString(opt.target) ) {
  296. me.$el.find(opt.target).addClass('msg-container');
  297. }
  298. },
  299. // Guess whether the form use ajax submit
  300. _guessAjax: function(form) {
  301. var me = this;
  302. if ( !(me.isAjaxSubmit = !!me.options.valid) ) {
  303. // if there is a "valid.form" event
  304. var events = ($._data || $.data)(form, 'events');
  305. me.isAjaxSubmit = issetEvent(events, 'valid', 'form') || issetEvent(events, 'submit', 'form-plugin');
  306. }
  307. function issetEvent(events, name, namespace) {
  308. return !!(
  309. events && events[name]
  310. && $.map(events[name], function(e){
  311. return ~e.namespace.indexOf(namespace) ? 1 : null;
  312. }).length )
  313. }
  314. },
  315. _initFields: function(fields) {
  316. var me = this, k, arr, i,
  317. clear = fields === null;
  318. // Processing field information
  319. if (clear) fields = me.fields;
  320. if ( isObject(fields) ) {
  321. for (k in fields) {
  322. if (~k.indexOf(',')) {
  323. arr = k.split(',');
  324. i = arr.length;
  325. while (i--) {
  326. initField(trim(arr[i]), fields[k]);
  327. }
  328. } else {
  329. initField(k, fields[k]);
  330. }
  331. }
  332. }
  333. // Parsing DOM rules
  334. me.$el.find(INPUT_SELECTOR).each(function() {
  335. me._parse(this);
  336. });
  337. function initField(k, v) {
  338. // delete a field from settings
  339. if ( v === null || clear ) {
  340. var el = me.elements[k];
  341. if (el) me._resetElement(el, true);
  342. delete me.fields[k];
  343. } else {
  344. me.fields[k] = new me.Field(k, isString(v) ? {rule: v} : v, me.fields[k]);
  345. }
  346. }
  347. },
  348. // Parsing a field
  349. _parse: function(el) {
  350. var me = this,
  351. field,
  352. key = el.name,
  353. display,
  354. timely,
  355. dataRule = attr(el, DATA_RULE);
  356. dataRule && attr(el, DATA_RULE, null);
  357. // If the field has passed the key as id mode, or it doesn't has a name
  358. if ( el.id && (
  359. ('#' + el.id in me.fields) ||
  360. !key ||
  361. // If dataRule and element are diffrent from old's, we use ID mode.
  362. (dataRule !== null && (field = me.fields[key]) && dataRule !== field.rule && el.id !== field.key)
  363. )
  364. ) {
  365. key = '#' + el.id;
  366. }
  367. // Generate id
  368. if (!key) {
  369. key = '#' + (el.id = 'N' + String(Math.random()).slice(-12));
  370. }
  371. field = me.getField(key, true);
  372. // The priority of passing parameter by DOM is higher than by JS.
  373. field.rule = dataRule || field.rule;
  374. if (display = attr(el, DATA_DISPLAY)) {
  375. field.display = display;
  376. }
  377. if (field.rule) {
  378. if ( attr(el, DATA_MUST) !== null || /\b(?:match|checked)\b/.test(field.rule) ) {
  379. field.must = true;
  380. }
  381. if ( /\brequired\b/.test(field.rule) ) {
  382. field.required = true;
  383. }
  384. if (timely = attr(el, DATA_TIMELY)) {
  385. field.timely = +timely;
  386. } else if (field.timely > 3) {
  387. attr(el, DATA_TIMELY, field.timely);
  388. }
  389. me._parseRule(field);
  390. field.old = {};
  391. }
  392. if ( isString(field.target) ) {
  393. attr(el, DATA_TARGET, field.target);
  394. }
  395. if ( isString(field.tip) ) {
  396. attr(el, DATA_TIP, field.tip);
  397. }
  398. return me.fields[key] = field;
  399. },
  400. // Parsing field rules
  401. _parseRule: function(field) {
  402. var arr = rDisplay.exec(field.rule);
  403. if (!arr) return;
  404. // current rule index
  405. field._i = 0;
  406. if (arr[1]) {
  407. field.display = arr[1];
  408. }
  409. if (arr[2]) {
  410. field._rules = [];
  411. arr[2].replace(rRules, function(){
  412. var args = arguments;
  413. args[4] = args[4] || args[5];
  414. field._rules.push({
  415. and: args[1] === '&',
  416. not: args[2] === '!',
  417. or: args[6] === '|',
  418. method: args[3],
  419. params: args[4] ? $.map( args[4].split(', '), trim ) : undefined
  420. });
  421. });
  422. }
  423. },
  424. // Verify a zone
  425. _multiValidate: function($inputs, doneCallback){
  426. var me = this,
  427. opt = me.options;
  428. me.hasError = false;
  429. if (opt.ignore) {
  430. $inputs = $inputs.not(opt.ignore);
  431. }
  432. $inputs.each(function() {
  433. me._validate(this);
  434. if (me.hasError && opt.stopOnError) {
  435. // stop the validation
  436. return false;
  437. }
  438. });
  439. // Need to wait for all fields validation complete, especially asynchronous validation
  440. if (doneCallback) {
  441. me.validating = true;
  442. $.when.apply(
  443. null,
  444. $.map(me.deferred, function(v){return v;})
  445. ).done(function(){
  446. doneCallback.call(me, !me.hasError);
  447. me.validating = false;
  448. });
  449. }
  450. // If the form does not contain asynchronous validation, the return value is correct.
  451. // Otherwise, you should detect form validation result through "doneCallback".
  452. return !$.isEmptyObject(me.deferred) ? undefined : !me.hasError;
  453. },
  454. // Validate the whole form
  455. _submit: function(e) {
  456. var me = this,
  457. opt = me.options,
  458. form = e.target,
  459. canSubmit = e.type === 'submit' && form.tagName === 'FORM' && !e.isDefaultPrevented();
  460. e.preventDefault();
  461. if (
  462. novalidateonce && ~(novalidateonce = false) ||
  463. // Prevent duplicate submission
  464. me.submiting ||
  465. // Receive the "validate" event only from the form.
  466. e.type === 'validate' && me.$el[0] !== form ||
  467. // trigger the beforeSubmit callback.
  468. isFunction(opt.beforeSubmit) && opt.beforeSubmit.call(me, form) === false
  469. ) {
  470. return;
  471. }
  472. if (me.isAjaxSubmit === undefined) {
  473. me._guessAjax(form);
  474. }
  475. me._debug('log', '\n<<< event: ' + e.type);
  476. me._reset();
  477. me.submiting = true;
  478. me._multiValidate(
  479. me.$el.find(INPUT_SELECTOR),
  480. function(isValid){
  481. var ret = (isValid || opt.debug === 2) ? 'valid' : 'invalid',
  482. errors;
  483. if (!isValid) {
  484. if (opt.focusInvalid) {
  485. // navigate to the error element
  486. me.$el.find('[' + ARIA_INVALID + ']:first').focus();
  487. }
  488. errors = $.map(me.errors, function(err){return err;});
  489. }
  490. // releasing submit
  491. me.submiting = false;
  492. me.isValid = isValid;
  493. // trigger callback and event
  494. isFunction(opt[ret]) && opt[ret].call(me, form, errors);
  495. me.$el.trigger(ret + CLS_NS_FORM, [form, errors]);
  496. me._debug('log', '>>> ' + ret);
  497. if (!isValid) return;
  498. // For jquery.form plugin
  499. if (me.vetoed) {
  500. $(form).ajaxSubmit(me.ajaxFormOptions);
  501. }
  502. else if (canSubmit && !me.isAjaxSubmit) {
  503. document.createElement('form').submit.call(form);
  504. }
  505. }
  506. );
  507. },
  508. _reset: function(e) {
  509. var me = this;
  510. me.errors = {};
  511. if (e) {
  512. me.reseting = true;
  513. me.$el.find(INPUT_SELECTOR).each( function(){
  514. me._resetElement(this);
  515. });
  516. delete me.reseting;
  517. }
  518. },
  519. _resetElement: function(el, all) {
  520. this._setClass(el, null);
  521. this.hideMsg(el);
  522. },
  523. // Handle events: "focusin/click"
  524. _focusin: function(e) {
  525. var me = this,
  526. opt = me.options,
  527. el = e.target,
  528. timely,
  529. msg;
  530. if ( me.validating || ( e.type==='click' && document.activeElement === el ) ) {
  531. return;
  532. }
  533. if (opt.focusCleanup) {
  534. if ( attr(el, ARIA_INVALID) === 'true' ) {
  535. me._setClass(el, null);
  536. me.hideMsg(el);
  537. }
  538. }
  539. msg = attr(el, DATA_TIP);
  540. if (msg) {
  541. me.showMsg(el, {
  542. type: 'tip',
  543. msg: msg
  544. });
  545. } else {
  546. if (attr(el, DATA_RULE)) {
  547. me._parse(el);
  548. }
  549. if (timely = attr(el, DATA_TIMELY)) {
  550. if ( timely === 8 || timely === 9 ) {
  551. me._focusout(e);
  552. }
  553. }
  554. }
  555. },
  556. // Handle events: "focusout/validate/keyup/click/change/input/compositionstart/compositionend"
  557. _focusout: function(e) {
  558. var me = this,
  559. opt = me.options,
  560. el = e.target,
  561. etype = e.type,
  562. etype0,
  563. focusin = etype === 'focusin',
  564. special = etype === 'validate',
  565. elem,
  566. field,
  567. old,
  568. value,
  569. timestamp,
  570. key, specialKey,
  571. timely,
  572. timer = 0;
  573. if (etype === 'compositionstart') {
  574. me.pauseValidate = true;
  575. }
  576. if (etype === 'compositionend') {
  577. me.pauseValidate = false;
  578. }
  579. if (me.pauseValidate) {
  580. return;
  581. }
  582. // For checkbox and radio
  583. elem = el.name && _checkable(el) ? me.$el.find('input[name="'+ el.name +'"]').get(0) : el;
  584. // Get field
  585. if (!(field = me.getField(elem)) || !field.rule) {
  586. return;
  587. }
  588. // Cache event type
  589. etype0 = field._e;
  590. field._e = etype;
  591. timely = field.timely;
  592. if (!special) {
  593. if (!timely || (_checkable(el) && etype !== 'click')) {
  594. return;
  595. }
  596. value = field.getValue();
  597. // not validate field unless fill a value
  598. if ( field.ignoreBlank && !value && !focusin ) {
  599. me.hideMsg(el);
  600. return;
  601. }
  602. if ( etype === 'focusout' ) {
  603. if (etype0 === 'change') {
  604. return;
  605. }
  606. if ( timely === 2 || timely === 8 ) {
  607. old = field.old;
  608. if (value && old) {
  609. if (field.isValid && !old.showOk) {
  610. me.hideMsg(el);
  611. } else {
  612. me._makeMsg(el, field, old);
  613. }
  614. } else {
  615. return;
  616. }
  617. }
  618. }
  619. else {
  620. if ( timely < 2 && !e.data ) {
  621. return;
  622. }
  623. // mark timestamp to reduce the frequency of the received event
  624. timestamp = +new Date();
  625. if ( timestamp - (el._ts || 0) < 100 ) {
  626. return;
  627. }
  628. el._ts = timestamp;
  629. // handle keyup
  630. if ( etype === 'keyup' ) {
  631. if (etype0 === 'input') {
  632. return;
  633. }
  634. key = e.keyCode;
  635. specialKey = {
  636. 8: 1, // Backspace
  637. 9: 1, // Tab
  638. 16: 1, // Shift
  639. 32: 1, // Space
  640. 46: 1 // Delete
  641. };
  642. // only gets focus, no validation
  643. if ( key === 9 && !value ) {
  644. return;
  645. }
  646. // do not validate, if triggered by these keys
  647. if ( key < 48 && !specialKey[key] ) {
  648. return;
  649. }
  650. }
  651. if ( !focusin ) {
  652. // keyboard events, reducing the frequency of validation
  653. timer = timely <100 ? (etype === 'click' || el.tagName === 'SELECT') ? 0 : 400 : timely;
  654. }
  655. }
  656. }
  657. // if the current field is ignored
  658. if ( opt.ignore && $(el).is(opt.ignore) ) {
  659. return;
  660. }
  661. clearTimeout(field._t);
  662. if (timer) {
  663. field._t = setTimeout(function() {
  664. me._validate(el, field);
  665. }, timer);
  666. } else {
  667. if (special) field.old = {};
  668. me._validate(el, field);
  669. }
  670. },
  671. _setClass: function(el, isValid) {
  672. var $el = $(el), opt = this.options;
  673. if (opt.bindClassTo) {
  674. $el = $el.closest(opt.bindClassTo);
  675. }
  676. $el.removeClass( opt.invalidClass + ' ' + opt.validClass );
  677. if (isValid !== null) {
  678. $el.addClass( isValid ? opt.validClass : opt.invalidClass );
  679. }
  680. },
  681. _showmsg: function(e, type, msg) {
  682. var me = this,
  683. el = e.target;
  684. if ( me.$el.is(el) ) {
  685. if (isObject(type)) {
  686. me.showMsg(type)
  687. }
  688. else if ( type === 'tip' ) {
  689. me.$el.find(INPUT_SELECTOR +'['+ DATA_TIP +']', el).each(function(){
  690. me.showMsg(this, {type: type, msg: msg});
  691. });
  692. }
  693. }
  694. else {
  695. me.showMsg(el, {type: type, msg: msg});
  696. }
  697. },
  698. _hidemsg: function(e) {
  699. var $el = $(e.target);
  700. if ( $el.is(INPUT_SELECTOR) ) {
  701. this.hideMsg($el);
  702. }
  703. },
  704. // Validated a field
  705. _validatedField: function(el, field, ret) {
  706. var me = this,
  707. opt = me.options,
  708. isValid = field.isValid = ret.isValid = !!ret.isValid,
  709. callback = isValid ? 'valid' : 'invalid';
  710. ret.key = field.key;
  711. ret.ruleName = field._r;
  712. ret.id = el.id;
  713. ret.value = field.value;
  714. me.elements[field.key] = ret.element = el;
  715. me.isValid = me.$el[0].isValid = isValid ? me.isFormValid() : isValid;
  716. if (isValid) {
  717. ret.type = 'ok';
  718. } else {
  719. if (me.submiting) {
  720. me.errors[field.key] = ret.msg;
  721. }
  722. me.hasError = true;
  723. }
  724. // cache result
  725. field.old = ret;
  726. // trigger callback
  727. isFunction(field[callback]) && field[callback].call(me, el, ret);
  728. isFunction(opt.validation) && opt.validation.call(me, el, ret);
  729. // trigger event
  730. $(el).attr( ARIA_INVALID, isValid ? null : true )
  731. .trigger( callback + CLS_NS_FIELD, [ret, me] );
  732. me.$el.triggerHandler('validation', [ret, me]);
  733. if (me.checkOnly) return;
  734. // set className
  735. me._setClass(el, ret.skip || ret.type === 'tip' ? null : isValid);
  736. me._makeMsg.apply(me, arguments);
  737. },
  738. _makeMsg: function(el, field, ret) {
  739. // show or hide the message
  740. if (field.msgMaker) {
  741. ret = $.extend({}, ret);
  742. if (field._e === 'focusin') {
  743. ret.type = 'tip';
  744. }
  745. this[ ret.showOk || ret.msg || ret.type === 'tip' ? 'showMsg' : 'hideMsg' ](el, ret, field);
  746. }
  747. },
  748. // Validated a rule
  749. _validatedRule: function(el, field, ret, msgOpt) {
  750. field = field || me.getField(el);
  751. msgOpt = msgOpt || {};
  752. var me = this,
  753. msg,
  754. rule,
  755. method = field._r,
  756. timely = field.timely,
  757. special = timely === 9 || timely === 8,
  758. transfer,
  759. temp,
  760. isValid = false;
  761. // use null to break validation from a field
  762. if (ret === null) {
  763. me._validatedField(el, field, {isValid: true, skip: true});
  764. field._i = 0;
  765. return;
  766. }
  767. else if (ret === undefined) {
  768. transfer = true;
  769. }
  770. else if (ret === true || ret === '') {
  771. isValid = true;
  772. }
  773. else if (isString(ret)) {
  774. msg = ret;
  775. }
  776. else if (isObject(ret)) {
  777. if (ret.error) {
  778. msg = ret.error;
  779. } else {
  780. msg = ret.ok;
  781. isValid = true;
  782. }
  783. }
  784. else {
  785. isValid = !!ret
  786. }
  787. rule = field._rules[field._i];
  788. if (rule.not) {
  789. msg = undefined;
  790. isValid = method === 'required' || !isValid;
  791. }
  792. if (rule.or) {
  793. if (isValid) {
  794. while ( field._i < field._rules.length && field._rules[field._i].or ) {
  795. field._i++;
  796. }
  797. } else {
  798. transfer = true;
  799. }
  800. }
  801. else if (rule.and) {
  802. if (!field.isValid) transfer = true;
  803. }
  804. if (transfer) {
  805. isValid = true;
  806. }
  807. // message analysis, and throw rule level event
  808. else {
  809. if (isValid) {
  810. if (field.showOk !== false) {
  811. temp = attr(el, DATA_OK);
  812. msg = temp === null ? isString(field.ok) ? field.ok : msg : temp;
  813. if (!isString(msg) && isString(field.showOk)) {
  814. msg = field.showOk;
  815. }
  816. if (isString(msg)) {
  817. msgOpt.showOk = isValid;
  818. }
  819. }
  820. }
  821. if (!isValid || special) {
  822. /* rule message priority:
  823. 1. custom DOM message
  824. 2. custom field message;
  825. 3. global defined message;
  826. 4. rule returned message;
  827. 5. default message;
  828. */
  829. msg = (_getDataMsg(el, field, msg || rule.msg || me.messages[method]) || me.messages.fallback).replace(/\{0\|?([^\}]*)\}/, function(m, defaultDisplay){
  830. return me._getDisplay(el, field.display) || defaultDisplay || me.messages[0];
  831. });
  832. }
  833. if (!isValid) field.isValid = isValid;
  834. msgOpt.msg = msg;
  835. $(el).trigger( (isValid ? 'valid' : 'invalid') + CLS_NS_RULE, [method, msg]);
  836. }
  837. if (special && (!transfer || rule.and)) {
  838. if (!isValid && !field._m) field._m = msg;
  839. field._v = field._v || [];
  840. field._v.push({
  841. type: isValid ? !transfer ? 'ok' : 'tip' : 'error',
  842. msg: msg || rule.msg
  843. });
  844. }
  845. me._debug('log', ' ' + field._i + ': ' + method + ' => ' + (isValid || msg));
  846. // the current rule has passed, continue to validate
  847. if ( (isValid || special) && field._i < field._rules.length - 1) {
  848. field._i++;
  849. me._checkRule(el, field);
  850. }
  851. // field was invalid, or all fields was valid
  852. else {
  853. field._i = 0;
  854. if (special) {
  855. msgOpt.isValid = field.isValid;
  856. msgOpt.result = field._v;
  857. msgOpt.msg = field._m || '';
  858. if (!field.value && (field._e === 'focusin')) {
  859. msgOpt.type = 'tip';
  860. }
  861. } else {
  862. msgOpt.isValid = isValid;
  863. }
  864. me._validatedField(el, field, msgOpt);
  865. delete field._m;
  866. delete field._v;
  867. }
  868. },
  869. // Verify a rule form a field
  870. _checkRule: function(el, field) {
  871. var me = this,
  872. ret,
  873. fn,
  874. old,
  875. key = field.key,
  876. rule = field._rules[field._i],
  877. method = rule.method,
  878. params = rule.params;
  879. // request has been sent, wait it
  880. if (me.submiting && me.deferred[key]) {
  881. return;
  882. }
  883. old = field.old;
  884. field._r = method;
  885. if (old && !field.must && !rule.must && rule.result !== undefined &&
  886. old.ruleName === method && old.id === el.id &&
  887. field.value && old.value === field.value )
  888. {
  889. // get result from cache
  890. ret = rule.result;
  891. }
  892. else {
  893. // get result from current rule
  894. fn = _getDataRule(el, method) || me.rules[method] || noop;
  895. ret = fn.call(field, el, params, field);
  896. if (fn.msg) rule.msg = fn.msg;
  897. }
  898. // asynchronous validation
  899. if (isObject(ret) && isFunction(ret.then)) {
  900. me.deferred[key] = ret;
  901. // whether the field valid is unknown
  902. field.isValid = undefined;
  903. // show loading message
  904. !me.checkOnly && me.showMsg(el, {
  905. type: 'loading',
  906. msg: me.messages.loading
  907. }, field);
  908. // waiting to parse the response data
  909. ret.then(
  910. function(d, textStatus, jqXHR) {
  911. var data = trim(jqXHR.responseText),
  912. result,
  913. dataFilter = field.dataFilter;
  914. // detect if data is json or jsonp format
  915. if (/jsonp?/.test(this.dataType)) {
  916. data = d;
  917. } else if (data.charAt(0) === '{') {
  918. data = $.parseJSON(data);
  919. }
  920. // filter data
  921. result = dataFilter.call(this, data, field);
  922. if (result === undefined) result = dataFilter.call(this, data.data, field);
  923. rule.data = this.data;
  924. rule.result = field.old ? result : undefined;
  925. me._validatedRule(el, field, result);
  926. },
  927. function(jqXHR, textStatus){
  928. me._validatedRule(el, field, me.messages[textStatus] || textStatus);
  929. }
  930. ).always(function(){
  931. delete me.deferred[key];
  932. });
  933. }
  934. // other result
  935. else {
  936. me._validatedRule(el, field, ret);
  937. }
  938. },
  939. // Processing the validation
  940. _validate: function(el, field) {
  941. var me = this;
  942. // doesn't validate the element that has "disabled" or "novalidate" attribute
  943. if ( el.disabled || attr(el, NOVALIDATE) !== null ) {
  944. return;
  945. }
  946. field = field || me.getField(el);
  947. if (!field) return;
  948. if (!field._rules) me._parse(el);
  949. if (!field._rules) return;
  950. me._debug('info', field.key);
  951. field.isValid = true;
  952. field.element = el;
  953. // Cache the value
  954. field.value = field.getValue();
  955. // if the field is not required, and that has a blank value
  956. if (!field.required && !field.must && !field.value) {
  957. if (!_checkable(el)) {
  958. me._validatedField(el, field, {isValid: true});
  959. return true;
  960. }
  961. }
  962. me._checkRule(el, field);
  963. return field.isValid;
  964. },
  965. _debug: function(type, messages) {
  966. if (window.console && this.options.debug) {
  967. console[type](messages);
  968. }
  969. },
  970. /**
  971. * Detecting whether the value of an element that matches a rule
  972. *
  973. * @method test
  974. * @param {Element} el - input element
  975. * @param {String} rule - rule name
  976. */
  977. test: function(el, rule) {
  978. var me = this,
  979. ret,
  980. parts = rRule.exec(rule),
  981. field,
  982. method,
  983. params;
  984. if (parts) {
  985. method = parts[1];
  986. if (method in me.rules) {
  987. params = parts[2] || parts[3];
  988. params = params ? params.split(', ') : undefined;
  989. field = me.getField(el, true);
  990. field._r = method;
  991. field.value = field.getValue();
  992. ret = me.rules[method].call(field, el, params);
  993. }
  994. }
  995. return ret === true || ret === undefined || ret === null;
  996. },
  997. _getDisplay: function(el, str) {
  998. return !isString(str) ? isFunction(str) ? str.call(this, el) : '' : str;
  999. },
  1000. _getMsgOpt: function(obj, field) {
  1001. var opt = field ? field : this.options;
  1002. return $.extend({
  1003. type: 'error',
  1004. pos: _getPos(opt.msgClass),
  1005. target: opt.target,
  1006. wrapper: opt.msgWrapper,
  1007. style: opt.msgStyle,
  1008. cls: opt.msgClass,
  1009. arrow: opt.msgArrow,
  1010. icon: opt.msgIcon
  1011. }, isString(obj) ? {msg: obj} : obj);
  1012. },
  1013. _getMsgDOM: function(el, msgOpt) {
  1014. var $el = $(el), $msgbox, datafor, tgt, container;
  1015. if ( $el.is(INPUT_SELECTOR) ) {
  1016. tgt = msgOpt.target || attr(el, DATA_TARGET);
  1017. if (tgt) {
  1018. tgt = !isFunction(tgt) ? tgt.charAt(0) === '#' ? $(tgt) : this.$el.find(tgt) : tgt.call(this, el);
  1019. if (tgt.length) {
  1020. if ( tgt.is(INPUT_SELECTOR) ) {
  1021. $el = tgt
  1022. el = tgt.get(0);
  1023. } else if ( tgt.hasClass(CLS_MSG_BOX) ) {
  1024. $msgbox = tgt;
  1025. } else {
  1026. container = tgt;
  1027. }
  1028. }
  1029. }
  1030. if (!$msgbox) {
  1031. datafor = (!_checkable(el) || !el.name) && el.id ? el.id : el.name;
  1032. $msgbox = (container || this.$el).find(msgOpt.wrapper + '.' + CLS_MSG_BOX + '[for="' + datafor + '"]');
  1033. }
  1034. } else {
  1035. $msgbox = $el;
  1036. }
  1037. // Create new message box
  1038. if (!msgOpt.hide && !$msgbox.length) {
  1039. $msgbox = $('<'+ msgOpt.wrapper + '>').attr({
  1040. 'class': CLS_MSG_BOX + (msgOpt.cls ? ' ' + msgOpt.cls : ''),
  1041. 'style': msgOpt.style || undefined,
  1042. 'for': datafor
  1043. });
  1044. if (container) {
  1045. $msgbox.appendTo(container);
  1046. } else {
  1047. if ( _checkable(el) ) {
  1048. var $parent = $el.parent();
  1049. $msgbox.appendTo( $parent.is('label') ? $parent.parent() : $parent );
  1050. } else {
  1051. $msgbox[!msgOpt.pos || msgOpt.pos === 'right' ? 'insertAfter' : 'insertBefore']($el);
  1052. }
  1053. }
  1054. }
  1055. return $msgbox;
  1056. },
  1057. /**
  1058. * Show validation message
  1059. *
  1060. * @method showMsg
  1061. * @param {Element} el - input element
  1062. * @param {Object} msgOpt
  1063. */
  1064. showMsg: function(el, msgOpt, /*INTERNAL*/ field) {
  1065. if (!el) return;
  1066. var me = this,
  1067. opt = me.options,
  1068. msgShow,
  1069. msgMaker,
  1070. temp,
  1071. $msgbox;
  1072. if (isObject(el) && !el.jquery && !msgOpt) {
  1073. $.each(el, function(key, msg) {
  1074. var el = me.elements[key] || me.$el.find(_key2selector(key))[0];
  1075. me.showMsg(el, msg);
  1076. });
  1077. return;
  1078. }
  1079. if ($(el).is(INPUT_SELECTOR)) {
  1080. field = field || me.getField(el);
  1081. }
  1082. if (!(msgMaker = (field || opt).msgMaker)) {
  1083. return;
  1084. }
  1085. msgOpt = me._getMsgOpt(msgOpt, field);
  1086. el = (el.name && _checkable(el) ? me.$el.find('input[name="'+ el.name +'"]') : $(el)).get(0);
  1087. // ok or tip
  1088. if (!msgOpt.msg && msgOpt.type !== 'error') {
  1089. temp = attr(el, 'data-' + msgOpt.type);
  1090. if (temp !== null) msgOpt.msg = temp;
  1091. }
  1092. if ( !isString(msgOpt.msg) ) {
  1093. return;
  1094. }
  1095. $msgbox = me._getMsgDOM(el, msgOpt);
  1096. !rPos.test($msgbox[0].className) && $msgbox.addClass(msgOpt.cls);
  1097. if ( isIE === 6 && msgOpt.pos === 'bottom' ) {
  1098. $msgbox[0].style.marginTop = $(el).outerHeight() + 'px';
  1099. }
  1100. $msgbox.html( msgMaker.call(me, msgOpt) )[0].style.display = '';
  1101. if (isFunction(msgShow = field && field.msgShow || opt.msgShow)) {
  1102. msgShow.call(me, $msgbox, msgOpt.type);
  1103. }
  1104. },
  1105. /**
  1106. * Hide validation message
  1107. *
  1108. * @method hideMsg
  1109. * @param {Element} el - input element
  1110. * @param {Object} msgOpt optional
  1111. */
  1112. hideMsg: function(el, msgOpt, /*INTERNAL*/ field) {
  1113. var me = this,
  1114. opt = me.options,
  1115. msgHide,
  1116. $msgbox;
  1117. el = $(el).get(0);
  1118. if ($(el).is(INPUT_SELECTOR)) {
  1119. field = field || me.getField(el);
  1120. if (field) {
  1121. if (field.isValid || me.reseting) attr(el, ARIA_INVALID, null);
  1122. }
  1123. }
  1124. msgOpt = me._getMsgOpt(msgOpt, field);
  1125. msgOpt.hide = true;
  1126. $msgbox = me._getMsgDOM(el, msgOpt);
  1127. if (!$msgbox.length) return;
  1128. if ( isFunction(msgHide = field && field.msgHide || opt.msgHide) ) {
  1129. msgHide.call(me, $msgbox, msgOpt.type);
  1130. } else {
  1131. $msgbox[0].style.display = 'none';
  1132. $msgbox[0].innerHTML = '';
  1133. }
  1134. },
  1135. /**
  1136. * Get field information
  1137. *
  1138. * @method getField
  1139. * @param {Element} - input element
  1140. * @return {Object} field
  1141. */
  1142. getField: function(el, must) {
  1143. var me = this,
  1144. key,
  1145. field;
  1146. if (isString(el)) {
  1147. key = el;
  1148. el = undefined;
  1149. } else {
  1150. if (attr(el, DATA_RULE)) {
  1151. return me._parse(el);
  1152. }
  1153. if (el.id && '#' + el.id in me.fields || !el.name) {
  1154. key = '#' + el.id;
  1155. } else {
  1156. key = el.name;
  1157. }
  1158. }
  1159. if ( (field = me.fields[key]) || must && (field = new me.Field(key)) ) {
  1160. field.element = el;
  1161. }
  1162. return field;
  1163. },
  1164. /**
  1165. * Config a field
  1166. *
  1167. * @method: setField
  1168. * @param {String} key
  1169. * @param {Object} obj
  1170. */
  1171. setField: function(key, obj) {
  1172. var fields = {};
  1173. if (!key) return;
  1174. // update this field
  1175. if (isString(key)) {
  1176. fields[key] = obj;
  1177. }
  1178. // update fields
  1179. else {
  1180. fields = key;
  1181. }
  1182. this._initFields(fields);
  1183. },
  1184. /**
  1185. * Detecting whether the form is valid
  1186. *
  1187. * @method isFormValid
  1188. * @return {Boolean}
  1189. */
  1190. isFormValid: function() {
  1191. var fields = this.fields, k, field;
  1192. for (k in fields) {
  1193. field = fields[k];
  1194. if (!field._rules || !field.required && !field.must && !field.value) continue;
  1195. if (!field.isValid) return false;
  1196. }
  1197. return true;
  1198. },
  1199. /**
  1200. * Prevent submission form
  1201. *
  1202. * @method holdSubmit
  1203. * @param {Boolean} hold - If set to false, will release the hold
  1204. */
  1205. holdSubmit: function(hold) {
  1206. this.submiting = hold === undefined || hold;
  1207. },
  1208. /**
  1209. * Clean validation messages
  1210. *
  1211. * @method cleanUp
  1212. */
  1213. cleanUp: function() {
  1214. this._reset(1);
  1215. },
  1216. /**
  1217. * Destroy the validation
  1218. *
  1219. * @method destroy
  1220. */
  1221. destroy: function() {
  1222. this._reset(1);
  1223. this.$el.off(CLS_NS).removeData(NS);
  1224. attr(this.$el[0], NOVALIDATE, this._NOVALIDATE);
  1225. }
  1226. };
  1227. /**
  1228. * Create Field Factory
  1229. *
  1230. * @class
  1231. * @param {Object} context
  1232. * @return {Function} Factory
  1233. */
  1234. function _createFieldFactory(context) {
  1235. function FieldFactory() {
  1236. var options = this.options;
  1237. for (var i in options) {
  1238. if (i in fieldDefaults) this[i] = options[i];
  1239. }
  1240. $.extend(this, {
  1241. _valHook: function() {
  1242. return this.element.contentEditable === 'true' ? 'text' : 'val';
  1243. },
  1244. getValue: function() {
  1245. var elem = this.element;
  1246. if (elem.type === 'number' && elem.validity && elem.validity.badInput) {
  1247. return 'NaN';
  1248. }
  1249. return $(elem)[this._valHook()]();
  1250. },
  1251. setValue: function(value) {
  1252. $(this.element)[this._valHook()](this.value = value);
  1253. },
  1254. // Get a range of validation messages
  1255. getRangeMsg: function(value, params, suffix) {
  1256. if (!params) return;
  1257. var me = this,
  1258. msg = me.messages[me._r] || '',
  1259. result,
  1260. p = params[0].split('~'),
  1261. e = params[1] === 'false',
  1262. a = p[0],
  1263. b = p[1],
  1264. c = 'rg',
  1265. args = [''],
  1266. isNumber = trim(value) && +value === +value;
  1267. function compare(large, small) {
  1268. return !e ? large >= small : large > small;
  1269. }
  1270. if (p.length === 2) {
  1271. if (a && b) {
  1272. if (isNumber && compare(value, +a) && compare(+b, value)) {
  1273. result = true;
  1274. }
  1275. args = args.concat(p);
  1276. c = e ? 'gtlt' : 'rg';
  1277. }
  1278. else if (a && !b) {
  1279. if (isNumber && compare(value, +a)) {
  1280. result = true;
  1281. }
  1282. args.push(a);
  1283. c = e ? 'gt' : 'gte';
  1284. }
  1285. else if (!a && b) {
  1286. if (isNumber && compare(+b, value)) {
  1287. result = true;
  1288. }
  1289. args.push(b);
  1290. c = e ? 'lt' : 'lte';
  1291. }
  1292. }
  1293. else {
  1294. if (value === +a) {
  1295. result = true;
  1296. }
  1297. args.push(a);
  1298. c = 'eq';
  1299. }
  1300. if (msg) {
  1301. if (suffix && msg[c + suffix]) {
  1302. c += suffix;
  1303. }
  1304. args[0] = msg[c];
  1305. }
  1306. return result || me._rules && ( me._rules[me._i].msg = me.renderMsg.apply(null, args) );
  1307. },
  1308. // Render message template
  1309. renderMsg: function() {
  1310. var args = arguments,
  1311. tpl = args[0],
  1312. i = args.length;
  1313. if (!tpl) return;
  1314. while (--i) {
  1315. tpl = tpl.replace('{' + i + '}', args[i]);
  1316. }
  1317. return tpl;
  1318. }
  1319. });
  1320. }
  1321. function Field(key, obj, oldField) {
  1322. this.key = key;
  1323. this.validator = context;
  1324. $.extend(this, oldField, obj);
  1325. }
  1326. FieldFactory.prototype = context;
  1327. Field.prototype = new FieldFactory();
  1328. return Field;
  1329. }
  1330. /**
  1331. * Create Rules
  1332. *
  1333. * @class
  1334. * @param {Object} obj rules
  1335. * @param {Object} context context
  1336. */
  1337. function Rules(obj, context) {
  1338. if (!isObject(obj)) return;
  1339. var k, that = context ? context === true ? this : context : Rules.prototype;
  1340. for (k in obj) {
  1341. if (_checkRuleName(k)) {
  1342. that[k] = _getRule(obj[k]);
  1343. }
  1344. }
  1345. }
  1346. /**
  1347. * Create Messages
  1348. *
  1349. * @class
  1350. * @param {Object} obj rules
  1351. * @param {Object} context context
  1352. */
  1353. function Messages(obj, context) {
  1354. if (!isObject(obj)) return;
  1355. var k, that = context ? context === true ? this : context : Messages.prototype;
  1356. for (k in obj) {
  1357. that[k] = obj[k];
  1358. }
  1359. }
  1360. // Rule converted factory
  1361. function _getRule(fn) {
  1362. switch ($.type(fn)) {
  1363. case 'function':
  1364. return fn;
  1365. case 'array':
  1366. var f = function() {
  1367. return fn[0].test(this.value) || fn[1] || false;
  1368. };
  1369. f.msg = fn[1];
  1370. return f;
  1371. case 'regexp':
  1372. return function() {
  1373. return fn.test(this.value);
  1374. };
  1375. }
  1376. }
  1377. // Get instance by an element
  1378. function _getInstance(el) {
  1379. var wrap, k, options;
  1380. if (!el || !el.tagName) return;
  1381. switch (el.tagName) {
  1382. case 'INPUT':
  1383. case 'SELECT':
  1384. case 'TEXTAREA':
  1385. case 'BUTTON':
  1386. case 'FIELDSET':
  1387. wrap = el.form || $(el).closest('.' + CLS_WRAPPER);
  1388. break;
  1389. case 'FORM':
  1390. wrap = el;
  1391. break;
  1392. default:
  1393. wrap = $(el).closest('.' + CLS_WRAPPER);
  1394. }
  1395. for (k in preinitialized) {
  1396. if ($(wrap).is(k)) {
  1397. options = preinitialized[k];
  1398. break;
  1399. }
  1400. }
  1401. return $(wrap).data(NS) || $(wrap)[NS](options).data(NS);
  1402. }
  1403. // Get custom rules on the node
  1404. function _getDataRule(el, method) {
  1405. var fn = trim(attr(el, DATA_RULE + '-' + method));
  1406. if ( fn && (fn = new Function('return ' + fn)()) ) {
  1407. return _getRule(fn);
  1408. }
  1409. }
  1410. // Get custom messages on the node
  1411. function _getDataMsg(el, field, m) {
  1412. var msg = field.msg,
  1413. item = field._r;
  1414. if ( isObject(msg) ) msg = msg[item];
  1415. if ( !isString(msg) ) {
  1416. msg = attr(el, DATA_MSG + '-' + item) || attr(el, DATA_MSG) || ( m ? isString(m) ? m : m[item] : '');
  1417. }
  1418. return msg;
  1419. }
  1420. // Get message position
  1421. function _getPos(str) {
  1422. var pos;
  1423. if (str) pos = rPos.exec(str);
  1424. return pos && pos[0];
  1425. }
  1426. // Check whether the element is checkbox or radio
  1427. function _checkable(el) {
  1428. return el.tagName === 'INPUT' && el.type === 'checkbox' || el.type === 'radio';
  1429. }
  1430. // Parse date string to timestamp
  1431. function _parseDate(str) {
  1432. return Date.parse(str.replace(/\.|\-/g, '/'));
  1433. }
  1434. // Rule name only allows alphanumeric characters and underscores
  1435. function _checkRuleName(name) {
  1436. return /^\w+$/.test(name);
  1437. }
  1438. // Translate field key to jQuery selector.
  1439. function _key2selector(key) {
  1440. var isID = key.charAt(0) === '#';
  1441. key = key.replace(/([:.{(|)}/\[\]])/g, '\\$1');
  1442. return isID ? key : '[name="'+ key +'"]:first';
  1443. }
  1444. // Fixed a issue cause by refresh page in IE.
  1445. $(window).on('beforeunload', function(){
  1446. this.focus();
  1447. });
  1448. $(document)
  1449. .on('click', ':submit', function(){
  1450. var input = this, attrNode;
  1451. if (!input.form) return;
  1452. // Shim for "formnovalidate"
  1453. attrNode = input.getAttributeNode('formnovalidate');
  1454. if (attrNode && attrNode.nodeValue !== null || attr(input, NOVALIDATE)!== null) {
  1455. novalidateonce = true;
  1456. }
  1457. })
  1458. // Automatic initializing form validation
  1459. .on('focusin submit validate', 'form,.'+CLS_WRAPPER, function(e) {
  1460. if ( attr(this, NOVALIDATE) !== null ) return;
  1461. var $form = $(this), me;
  1462. if ( !$form.data(NS) && (me = _getInstance(this)) ) {
  1463. if ( !$.isEmptyObject(me.fields) ) {
  1464. // Execute event handler
  1465. if (e.type === 'focusin') {
  1466. me._focusin(e);
  1467. } else {
  1468. me._submit(e);
  1469. }
  1470. } else {
  1471. attr(this, NOVALIDATE, NOVALIDATE);
  1472. $form.off(CLS_NS).removeData(NS);
  1473. }
  1474. }
  1475. });
  1476. new Messages({
  1477. fallback: 'This field is not valid.',
  1478. loading: 'Validating...'
  1479. });
  1480. // Built-in rules (global)
  1481. new Rules({
  1482. /**
  1483. * required
  1484. *
  1485. * @example:
  1486. required
  1487. required(jqSelector)
  1488. required(anotherRule)
  1489. required(not, -1)
  1490. required(from, .contact)
  1491. */
  1492. required: function(element, params) {
  1493. var me = this,
  1494. val = trim(me.value),
  1495. isValid = true;
  1496. if (params) {
  1497. if ( params.length === 1 ) {
  1498. if ( !_checkRuleName(params[0]) ) {
  1499. if (!val && !$(params[0], me.$el).length ) {
  1500. return null;
  1501. }
  1502. }
  1503. else if ( me.rules[params[0]] ) {
  1504. if ( !val && !me.test(element, params[0]) ) {
  1505. return null;
  1506. }
  1507. me._r = 'required'
  1508. }
  1509. }
  1510. else if ( params[0] === 'not' ) {
  1511. $.each(params.slice(1), function() {
  1512. return (isValid = val !== trim(this));
  1513. });
  1514. }
  1515. else if ( params[0] === 'from' ) {
  1516. var $elements = me.$el.find(params[1]),
  1517. VALIDATED = '_validated_',
  1518. ret;
  1519. isValid = $elements.filter(function(){
  1520. var field = me.getField(this);
  1521. return field && !!trim(field.getValue());
  1522. }).length >= (params[2] || 1);
  1523. if (isValid) {
  1524. if (!val) ret = null;
  1525. } else {
  1526. ret = _getDataMsg($elements[0], me) || false;
  1527. }
  1528. if ( !$(element).data(VALIDATED) ) {
  1529. $elements.data(VALIDATED, 1).each(function(){
  1530. if (element !== this) {
  1531. me._validate(this);
  1532. }
  1533. }).removeData(VALIDATED);
  1534. }
  1535. return ret;
  1536. }
  1537. }
  1538. return isValid && !!val;
  1539. },
  1540. /**
  1541. * integer
  1542. *
  1543. * @example:
  1544. integer
  1545. integer[+]
  1546. integer[+0]
  1547. integer[-]
  1548. integer[-0]
  1549. */
  1550. integer: function(element, params) {
  1551. var re, z = '0|',
  1552. p = '[1-9]\\d*',
  1553. key = params ? params[0] : '*';
  1554. switch (key) {
  1555. case '+':
  1556. re = p;
  1557. break;
  1558. case '-':
  1559. re = '-' + p;
  1560. break;
  1561. case '+0':
  1562. re = z + p;
  1563. break;
  1564. case '-0':
  1565. re = z + '-' + p;
  1566. break;
  1567. default:
  1568. re = z + '-?' + p;
  1569. }
  1570. re = '^(?:' + re + ')$';
  1571. return new RegExp(re).test(this.value) || (this.messages.integer && this.messages.integer[key]);
  1572. },
  1573. /**
  1574. * match another field
  1575. *
  1576. * @example:
  1577. match[password] Match the password field (two values ​​must be the same)
  1578. match[eq, password] Ditto
  1579. match[neq, count] The value must be not equal to the value of the count field
  1580. match[lt, count] The value must be less than the value of the count field
  1581. match[lte, count] The value must be less than or equal to the value of the count field
  1582. match[gt, count] The value must be greater than the value of the count field
  1583. match[gte, count] The value must be greater than or equal to the value of the count field
  1584. match[gte, startDate, date]
  1585. match[gte, startTime, time]
  1586. **/
  1587. match: function(element, params) {
  1588. if (!params) return;
  1589. var me = this,
  1590. isValid = true,
  1591. a, b,
  1592. key, msg, type = 'eq', parser,
  1593. selector2, elem2, field2;
  1594. if (params.length === 1) {
  1595. key = params[0];
  1596. } else {
  1597. type = params[0];
  1598. key = params[1];
  1599. }
  1600. selector2 = _key2selector(key);
  1601. elem2 = me.$el.find(selector2)[0];
  1602. // If the compared field is not exist
  1603. if (!elem2) return;
  1604. field2 = me.getField(elem2);
  1605. a = me.value;
  1606. b = field2.getValue();
  1607. if (!me._match) {
  1608. me.$el.on('valid'+CLS_NS_FIELD+CLS_NS, selector2, function(){
  1609. $(element).trigger('validate');
  1610. });
  1611. me._match = field2._match = 1;
  1612. }
  1613. // If both fields are blank
  1614. if (!me.required && a === '' && b === '') {
  1615. return null;
  1616. }
  1617. parser = params[2];
  1618. if (parser) {
  1619. if (/^date(time)?$/i.test(parser)) {
  1620. a = _parseDate(a);
  1621. b = _parseDate(b);
  1622. } else if (parser === 'time') {
  1623. a = +a.replace(/:/g, '');
  1624. b = +b.replace(/:/g, '');
  1625. }
  1626. }
  1627. // If the compared field is incorrect, we only ensure that this field is correct.
  1628. if (type !== 'eq' && !isNaN(+a) && isNaN(+b)) {
  1629. return true;
  1630. }
  1631. switch (type) {
  1632. case 'lt':
  1633. isValid = +a < +b; break;
  1634. case 'lte':
  1635. isValid = +a <= +b; break;
  1636. case 'gte':
  1637. isValid = +a >= +b; break;
  1638. case 'gt':
  1639. isValid = +a > +b; break;
  1640. case 'neq':
  1641. isValid = a !== b; break;
  1642. default:
  1643. isValid = a === b;
  1644. }
  1645. return isValid || (
  1646. isObject(me.messages.match)
  1647. && me.messages.match[type].replace( '{1}', me._getDisplay( elem2, field2.display || key ) )
  1648. );
  1649. },
  1650. /**
  1651. * range numbers
  1652. *
  1653. * @example:
  1654. range[0~99] Number 0-99
  1655. range[0~] Number greater than or equal to 0
  1656. range[~100] Number less than or equal to 100
  1657. **/
  1658. range: function(element, params) {
  1659. return this.getRangeMsg(this.value, params);
  1660. },
  1661. /**
  1662. * how many checkbox or radio inputs that checked
  1663. *
  1664. * @example:
  1665. checked; no empty, same to required
  1666. checked[1~3] 1-3 items
  1667. checked[1~] greater than 1 item
  1668. checked[~3] less than 3 items
  1669. checked[3] 3 items
  1670. **/
  1671. checked: function(element, params) {
  1672. if ( !_checkable(element) ) return;
  1673. var me = this,
  1674. elem, count;
  1675. if (element.name) {
  1676. count = me.$el.find('input[name="' + element.name + '"]').filter(function() {
  1677. var el = this;
  1678. if (!elem && _checkable(el)) elem = el;
  1679. return !el.disabled && el.checked;
  1680. }).length;
  1681. } else {
  1682. elem = element;
  1683. count = elem.checked;
  1684. }
  1685. if (params) {
  1686. return me.getRangeMsg(count, params);
  1687. } else {
  1688. return !!count || _getDataMsg(elem, me, '') || me.messages.required || false;
  1689. }
  1690. },
  1691. /**
  1692. * length of a characters (You can pass the second parameter "true", will calculate the length in bytes)
  1693. *
  1694. * @example:
  1695. length[6~16] 6-16 characters
  1696. length[6~] Greater than 6 characters
  1697. length[~16] Less than 16 characters
  1698. length[~16, true] Less than 16 characters, non-ASCII characters calculating two-character
  1699. **/
  1700. length: function(element, params) {
  1701. var value = this.value,
  1702. len = (params[1] === 'true' ? value.replace(rDoubleBytes, 'xx') : value).length;
  1703. return this.getRangeMsg(len, params, (params[1] ? '_2' : ''));
  1704. },
  1705. /**
  1706. * remote validation
  1707. *
  1708. * @description
  1709. * remote([get:]url [, name1, [name2 ...]]);
  1710. * Adaptation three kinds of results (Front for the successful, followed by a failure):
  1711. 1. text:
  1712. '' 'Error Message'
  1713. 2. json:
  1714. {"ok": ""} {"error": "Error Message"}
  1715. 3. json wrapper:
  1716. {"status": 1, "data": {"ok": ""}} {"status": 1, "data": {"error": "Error Message"}}
  1717. * @example
  1718. The simplest: remote(path/to/server);
  1719. With parameters: remote(path/to/server, name1, name2, ...);
  1720. By GET: remote(get:path/to/server, name1, name2, ...);
  1721. Name proxy: remote(path/to/server, name1, proxyname2:name2, proxyname3:#id3, ...)
  1722. Query String remote(path/to/server, foo=1&bar=2, name1, name2, ...)
  1723. CORS remote(cors:path/to/server)
  1724. JSONP remote(jsonp:path/to/server)
  1725. */
  1726. remote: function(element, params) {
  1727. if (!params) return;
  1728. var me = this,
  1729. arr = rAjaxType.exec(params[0]),
  1730. rule = me._rules[me._i],
  1731. data = {},
  1732. queryString = '',
  1733. url = arr[3],
  1734. type = arr[2] || 'POST', // GET / POST
  1735. rType = (arr[1] || this.validator.options.remoteDataType || '').toLowerCase(), // CORS / JSONP
  1736. dataType;
  1737. rule.must = true;
  1738. data[element.name] = me.value;
  1739. // There are extra fields
  1740. if (params[1]) {
  1741. $.map(params.slice(1), function(name) {
  1742. var arr, key;
  1743. if (~name.indexOf('=')) {
  1744. queryString += '&' + name;
  1745. } else {
  1746. arr = name.split(':');
  1747. name = trim(arr[0]);
  1748. key = trim(arr[1]) || name;
  1749. data[ name ] = me.$el.find( _key2selector(key) ).val();
  1750. }
  1751. });
  1752. }
  1753. data = $.param(data) + queryString;
  1754. if (!me.must && rule.data && rule.data === data) {
  1755. return rule.result;
  1756. }
  1757. // Cross-domain request, force jsonp dataType
  1758. if (rType !== 'cors' && /^https?:/.test(url) && !~url.indexOf(location.host)) {
  1759. dataType = 'jsonp';
  1760. }
  1761. // Asynchronous validation need return jqXHR objects
  1762. return $.ajax({
  1763. url: url,
  1764. type: type,
  1765. data: data,
  1766. dataType: dataType
  1767. });
  1768. },
  1769. /**
  1770. * filter characters, direct filtration without prompting error (support custom regular expressions)
  1771. *
  1772. * @example
  1773. * filter filtering unsafe characters
  1774. * filter(regexp) filtering the "regexp" matched characters
  1775. */
  1776. filter: function(element, params) {
  1777. var value = this.value,
  1778. temp = value.replace( params ? (new RegExp('[' + params[0] + ']', 'gm')) : rUnsafe, '' );
  1779. if (temp !== value) this.setValue(temp);
  1780. }
  1781. });
  1782. /**
  1783. * Config global options
  1784. *
  1785. * @static config
  1786. * @param {Object} options
  1787. */
  1788. Validator.config = function(key, value) {
  1789. if (isObject(key)) {
  1790. $.each(key, _config);
  1791. }
  1792. else if (isString(key)) {
  1793. _config(key, value);
  1794. }
  1795. function _config(k, o) {
  1796. if (k === 'rules') {
  1797. new Rules(o);
  1798. }
  1799. else if (k === 'messages') {
  1800. new Messages(o);
  1801. }
  1802. else if (k in fieldDefaults) {
  1803. fieldDefaults[k] = o;
  1804. }
  1805. else {
  1806. defaults[k] = o;
  1807. }
  1808. }
  1809. };
  1810. /**
  1811. * Config themes
  1812. *
  1813. * @static setTheme
  1814. * @param {String|Object} name
  1815. * @param {Object} obj
  1816. * @example
  1817. .setTheme( themeName, themeOptions )
  1818. .setTheme( multiThemes )
  1819. */
  1820. Validator.setTheme = function(name, obj) {
  1821. if ( isObject(name) ) {
  1822. $.extend(true, themes, name);
  1823. }
  1824. else if ( isString(name) && isObject(obj) ) {
  1825. themes[name] = $.extend(themes[name], obj);
  1826. }
  1827. };
  1828. /**
  1829. * Resource loader
  1830. *
  1831. * @static load
  1832. * @param {String} str
  1833. * @example
  1834. .load('local=zh-CN') // load: local/zh-CN.js and jquery.validator.css
  1835. .load('local=zh-CN&css=') // load: local/zh-CN.js
  1836. .load('local&css') // load: local/en.js (set <html lang="en">) and jquery.validator.css
  1837. .load('local') // dito
  1838. */
  1839. Validator.load = function(str) {
  1840. if (!str) return;
  1841. var doc = document,
  1842. params = {},
  1843. node = doc.scripts[0],
  1844. dir, el, ONLOAD;
  1845. str.replace(/([^?=&]+)=([^&#]*)/g, function(m, key, value){
  1846. params[key] = value;
  1847. });
  1848. dir = params.dir || Validator.dir;
  1849. if (!Validator.css && params.css !== '') {
  1850. el = doc.createElement('link');
  1851. el.rel = 'stylesheet';
  1852. el.href = Validator.css = dir + 'jquery.validator.css';
  1853. node.parentNode.insertBefore(el, node);
  1854. }
  1855. if (!Validator.local && ~str.indexOf('local') && params.local !== '') {
  1856. Validator.local = (params.local || doc.documentElement.lang || 'en').replace('_','-');
  1857. Validator.pending = 1;
  1858. el = doc.createElement('script');
  1859. el.src = dir + 'local/' + Validator.local + '.js';
  1860. ONLOAD = 'onload' in el ? 'onload' : 'onreadystatechange';
  1861. el[ONLOAD] = function() {
  1862. if (!el.readyState || /loaded|complete/.test(el.readyState)) {
  1863. el = el[ONLOAD] = null;
  1864. delete Validator.pending;
  1865. $(window).triggerHandler('validatorready');
  1866. }
  1867. };
  1868. node.parentNode.insertBefore(el, node);
  1869. }
  1870. };
  1871. // Auto loading resources
  1872. (function(){
  1873. var scripts = document.scripts,
  1874. i = scripts.length, node, arr,
  1875. re = /(.*validator(?:\.min)?.js)(\?.*(?:local|css|dir)(?:=[\w\-]*)?)?/;
  1876. while (i-- && !arr) {
  1877. node = scripts[i];
  1878. arr = (node.hasAttribute ? node.src : node.getAttribute('src',4)||'').match(re);
  1879. }
  1880. if (!arr) return;
  1881. Validator.dir = arr[1].split('/').slice(0, -1).join('/')+'/';
  1882. Validator.load(arr[2]);
  1883. })();
  1884. return $[NS] = Validator;
  1885. }));