PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/files/jquery.h5validate/0.8.4/jquery.h5validate.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 553 lines | 353 code | 78 blank | 122 comment | 54 complexity | 4c772dd400c89a1562bb5eee9d14b4ad MD5 | raw file
  1. /**
  2. * h5Validate
  3. * @version v0.8.4
  4. * Using semantic versioning: http://semver.org/
  5. * @author Eric Hamilton http://ericleads.com/
  6. * @copyright 2010 - 2012 Eric Hamilton
  7. * Dual licensed under the MIT and GPL licenses:
  8. * http://www.opensource.org/licenses/mit-license.php
  9. * http://www.gnu.org/licenses/gpl.html
  10. *
  11. * Developed under the sponsorship of RootMusic, Zumba Fitness, LLC, and Rese Property Management
  12. */
  13. /*global jQuery, window, console */
  14. (function ($) {
  15. 'use strict';
  16. var console = window.console || function () {},
  17. h5 = { // Public API
  18. defaults : {
  19. debug: false,
  20. RODom: false,
  21. // HTML5-compatible validation pattern library that can be extended and/or overriden.
  22. patternLibrary : { //** TODO: Test the new regex patterns. Should I apply these to the new input types?
  23. // **TODO: password
  24. phone: /([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)/,
  25. // Shamelessly lifted from Scott Gonzalez via the Bassistance Validation plugin http://projects.scottsplayground.com/email_address_validation/
  26. email: /((([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?/,
  27. // Shamelessly lifted from Scott Gonzalez via the Bassistance Validation plugin http://projects.scottsplayground.com/iri/
  28. url: /(https?|ftp):\/\/(((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?/,
  29. // Number, including positive, negative, and floating decimal. Credit: bassistance
  30. number: /-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?/,
  31. // Date in ISO format. Credit: bassistance
  32. dateISO: /\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}/,
  33. alpha: /[a-zA-Z]+/,
  34. alphaNumeric: /\w+/,
  35. integer: /-?\d+/
  36. },
  37. // The prefix to use for dynamically-created class names.
  38. classPrefix: 'h5-',
  39. errorClass: 'ui-state-error', // No prefix for these.
  40. validClass: 'ui-state-valid', // "
  41. activeClass: 'active', // Prefix will get prepended.
  42. requiredClass: 'required',
  43. requiredAttribute: 'required',
  44. patternAttribute: 'pattern',
  45. // Attribute which stores the ID of the error container element (without the hash).
  46. errorAttribute: 'data-h5-errorid',
  47. // Events API
  48. customEvents: {
  49. 'validate': true
  50. },
  51. // Setup KB event delegation.
  52. kbSelectors: ':input:not(:button):not(:disabled):not(.novalidate)',
  53. focusout: true,
  54. focusin: false,
  55. change: true,
  56. keyup: false,
  57. activeKeyup: true,
  58. // Setup mouse event delegation.
  59. mSelectors: '[type="range"]:not(:disabled):not(.novalidate), :radio:not(:disabled):not(.novalidate), :checkbox:not(:disabled):not(.novalidate), select:not(:disabled):not(.novalidate), option:not(:disabled):not(.novalidate)',
  60. click: true,
  61. // What do we name the required .data variable?
  62. requiredVar: 'h5-required',
  63. // What do we name the pattern .data variable?
  64. patternVar: 'h5-pattern',
  65. stripMarkup: true,
  66. // Run submit related checks and prevent form submission if any fields are invalid?
  67. submit: true,
  68. // Move focus to the first invalid field on submit?
  69. focusFirstInvalidElementOnSubmit: true,
  70. // When submitting, validate elements that haven't been validated yet?
  71. validateOnSubmit: true,
  72. // Callback stubs
  73. invalidCallback: function () {},
  74. validCallback: function () {},
  75. // Elements to validate with allValid (only validating visible elements)
  76. allValidSelectors: ':input:visible:not(:button):not(:disabled):not(.novalidate)',
  77. // Mark field invalid.
  78. // ** TODO: Highlight labels
  79. // ** TODO: Implement setCustomValidity as per the spec:
  80. // http://www.whatwg.org/specs/web-apps/current-work/multipage/association-of-controls-and-forms.html#dom-cva-setcustomvalidity
  81. markInvalid: function markInvalid(options) {
  82. var $element = $(options.element),
  83. $errorID = $(options.errorID);
  84. $element.addClass(options.errorClass).removeClass(options.validClass);
  85. // User needs help. Enable active validation.
  86. $element.addClass(options.settings.activeClass);
  87. if ($errorID.length) { // These ifs are technically not needed, but improve server-side performance
  88. if ($element.attr('title')) {
  89. $errorID.text($element.attr('title'));
  90. }
  91. $errorID.show();
  92. }
  93. $element.data('valid', false);
  94. options.settings.invalidCallback.call(options.element, options.validity);
  95. return $element;
  96. },
  97. // Mark field valid.
  98. markValid: function markValid(options) {
  99. var $element = $(options.element),
  100. $errorID = $(options.errorID);
  101. $element.addClass(options.validClass).removeClass(options.errorClass);
  102. if ($errorID.length) {
  103. $errorID.hide();
  104. }
  105. $element.data('valid', true);
  106. options.settings.validCallback.call(options.element, options.validity);
  107. return $element;
  108. },
  109. // Unmark field
  110. unmark: function unmark(options) {
  111. var $element = $(options.element);
  112. $element.removeClass(options.errorClass).removeClass(options.validClass);
  113. $element.form.find("#" + options.element.id).removeClass(options.errorClass).removeClass(options.validClass);
  114. return $element;
  115. }
  116. }
  117. },
  118. // Aliases
  119. defaults = h5.defaults,
  120. patternLibrary = defaults.patternLibrary,
  121. createValidity = function createValidity(validity) {
  122. return $.extend({
  123. customError: validity.customError || false,
  124. patternMismatch: validity.patternMismatch || false,
  125. rangeOverflow: validity.rangeOverflow || false,
  126. rangeUnderflow: validity.rangeUnderflow || false,
  127. stepMismatch: validity.stepMismatch || false,
  128. tooLong: validity.tooLong || false,
  129. typeMismatch: validity.typeMismatch || false,
  130. valid: validity.valid || true,
  131. valueMissing: validity.valueMissing || false
  132. }, validity);
  133. },
  134. methods = {
  135. /**
  136. * Check the validity of the current field
  137. * @param {object} settings instance settings
  138. * @param {object} options
  139. * .revalidate - trigger validation function first?
  140. * @return {Boolean}
  141. */
  142. isValid: function (settings, options) {
  143. var $this = $(this);
  144. options = (settings && options) || {};
  145. // Revalidate defaults to true
  146. if (options.revalidate !== false) {
  147. $this.trigger('validate');
  148. }
  149. return $this.data('valid'); // get the validation result
  150. },
  151. allValid: function (config, options) {
  152. var valid = true,
  153. formValidity = [],
  154. $this = $(this),
  155. $allFields,
  156. $filteredFields,
  157. radioNames = [],
  158. getValidity = function getValidity(e, data) {
  159. data.e = e;
  160. formValidity.push(data);
  161. },
  162. settings = $.extend({}, config, options); // allow options to override settings
  163. options = options || {};
  164. $this.trigger('formValidate', {settings: $.extend(true, {}, settings)});
  165. // Make sure we're not triggering handlers more than we need to.
  166. $this.undelegate(settings.allValidSelectors,
  167. '.allValid', getValidity);
  168. $this.delegate(settings.allValidSelectors,
  169. 'validated.allValid', getValidity);
  170. $allFields = $this.find(settings.allValidSelectors);
  171. // Filter radio buttons with the same name and keep only one,
  172. // since they will be checked as a group by isValid()
  173. $filteredFields = $allFields.filter(function(index) {
  174. var name;
  175. if(this.tagName === "INPUT"
  176. && this.type === "radio") {
  177. name = this.name;
  178. if(radioNames[name] === true) {
  179. return false;
  180. }
  181. radioNames[name] = true;
  182. }
  183. return true;
  184. });
  185. $filteredFields.each(function () {
  186. var $this = $(this);
  187. valid = $this.h5Validate('isValid', options) && valid;
  188. });
  189. $this.trigger('formValidated', {valid: valid, elements: formValidity});
  190. return valid;
  191. },
  192. validate: function (settings) {
  193. // Get the HTML5 pattern attribute if it exists.
  194. // ** TODO: If a pattern class exists, grab the pattern from the patternLibrary, but the pattern attrib should override that value.
  195. var $this = $(this),
  196. pattern = $this.filter('[pattern]')[0] ? $this.attr('pattern') : false,
  197. // The pattern attribute must match the whole value, not just a subset:
  198. // "...as if it implied a ^(?: at the start of the pattern and a )$ at the end."
  199. re = new RegExp('^(?:' + pattern + ')$'),
  200. $radiosWithSameName = null,
  201. value = ($this.is('[type=checkbox]')) ?
  202. $this.is(':checked') : ($this.is('[type=radio]') ?
  203. // Cache all radio buttons (in the same form) with the same name as this one
  204. ($radiosWithSameName = $this.parents('form')
  205. // **TODO: escape the radio buttons' name before using it in the jQuery selector
  206. .find('input[name="' + $this.attr('name') + '"]'))
  207. .filter(':checked')
  208. .length > 0 : $this.val()),
  209. errorClass = settings.errorClass,
  210. validClass = settings.validClass,
  211. errorIDbare = $this.attr(settings.errorAttribute) || false, // Get the ID of the error element.
  212. errorID = errorIDbare ? '#' + errorIDbare.replace(/(:|\.|\[|\])/g,'\\$1') : false, // Add the hash for convenience. This is done in two steps to avoid two attribute lookups.
  213. required = false,
  214. validity = createValidity({element: this, valid: true}),
  215. $checkRequired = $('<input required>'),
  216. maxlength;
  217. /* If the required attribute exists, set it required to true, unless it's set 'false'.
  218. * This is a minor deviation from the spec, but it seems some browsers have falsey
  219. * required values if the attribute is empty (should be true). The more conformant
  220. * version of this failed sanity checking in the browser environment.
  221. * This plugin is meant to be practical, not ideologically married to the spec.
  222. */
  223. // Feature fork
  224. if ($checkRequired.filter('[required]') && $checkRequired.filter('[required]').length) {
  225. required = ($this.filter('[required]').length && $this.attr('required') !== 'false');
  226. } else {
  227. required = ($this.attr('required') !== undefined);
  228. }
  229. if (settings.debug && window.console) {
  230. console.log('Validate called on "' + value + '" with regex "' + re + '". Required: ' + required); // **DEBUG
  231. console.log('Regex test: ' + re.test(value) + ', Pattern: ' + pattern); // **DEBUG
  232. }
  233. maxlength = parseInt($this.attr('maxlength'), 10);
  234. if (!isNaN(maxlength) && value.length > maxlength) {
  235. validity.valid = false;
  236. validity.tooLong = true;
  237. }
  238. if (required && !value) {
  239. validity.valid = false;
  240. validity.valueMissing = true;
  241. } else if (pattern && !re.test(value) && value) {
  242. validity.valid = false;
  243. validity.patternMismatch = true;
  244. } else {
  245. if (!settings.RODom) {
  246. settings.markValid({
  247. element: this,
  248. validity: validity,
  249. errorClass: errorClass,
  250. validClass: validClass,
  251. errorID: errorID,
  252. settings: settings
  253. });
  254. }
  255. }
  256. if (!validity.valid) {
  257. if (!settings.RODom) {
  258. settings.markInvalid({
  259. element: this,
  260. validity: validity,
  261. errorClass: errorClass,
  262. validClass: validClass,
  263. errorID: errorID,
  264. settings: settings
  265. });
  266. }
  267. }
  268. $this.trigger('validated', validity);
  269. // If it's a radio button, also validate the other radio buttons with the same name
  270. // (while making sure the call is not recursive)
  271. if($radiosWithSameName !== null
  272. && settings.alreadyCheckingRelatedRadioButtons !== true) {
  273. settings.alreadyCheckingRelatedRadioButtons = true;
  274. $radiosWithSameName
  275. .not($this)
  276. .trigger('validate');
  277. settings.alreadyCheckingRelatedRadioButtons = false;
  278. }
  279. },
  280. /**
  281. * Take the event preferences and delegate the events to selected
  282. * objects.
  283. *
  284. * @param {object} eventFlags The object containing event flags.
  285. *
  286. * @returns {element} The passed element (for method chaining).
  287. */
  288. delegateEvents: function (selectors, eventFlags, element, settings) {
  289. var events = {},
  290. key = 0,
  291. validate = function () {
  292. settings.validate.call(this, settings);
  293. };
  294. $.each(eventFlags, function (key, value) {
  295. if (value) {
  296. events[key] = key;
  297. }
  298. });
  299. // key = 0;
  300. for (key in events) {
  301. if (events.hasOwnProperty(key)) {
  302. $(element).delegate(selectors, events[key] + '.h5Validate', validate);
  303. }
  304. }
  305. return element;
  306. },
  307. /**
  308. * Prepare for event delegation.
  309. *
  310. * @param {object} settings The full plugin state, including
  311. * options.
  312. *
  313. * @returns {object} jQuery object for chaining.
  314. */
  315. bindDelegation: function (settings) {
  316. var $this = $(this),
  317. $forms;
  318. // Attach patterns from the library to elements.
  319. // **TODO: pattern / validation method matching should
  320. // take place inside the validate action.
  321. $.each(patternLibrary, function (key, value) {
  322. var pattern = value.toString();
  323. pattern = pattern.substring(1, pattern.length - 1);
  324. $('.' + settings.classPrefix + key).attr('pattern', pattern);
  325. });
  326. $forms = $this.filter('form')
  327. .add($this.find('form'))
  328. .add($this.parents('form'));
  329. $forms
  330. .attr('novalidate', 'novalidate')
  331. .submit(checkValidityOnSubmitHandler);
  332. $forms.find("input[formnovalidate][type='submit']").click(function(){
  333. $(this).closest("form").unbind('submit', checkValidityOnSubmitHandler);
  334. });
  335. return this.each(function () {
  336. var kbEvents = {
  337. focusout: settings.focusout,
  338. focusin: settings.focusin,
  339. change: settings.change,
  340. keyup: settings.keyup
  341. },
  342. mEvents = {
  343. click: settings.click
  344. },
  345. activeEvents = {
  346. keyup: settings.activeKeyup
  347. };
  348. settings.delegateEvents(':input', settings.customEvents, this, settings);
  349. settings.delegateEvents(settings.kbSelectors, kbEvents, this, settings);
  350. settings.delegateEvents(settings.mSelectors, mEvents, this, settings);
  351. settings.delegateEvents(settings.activeClassSelector, activeEvents, this, settings);
  352. settings.delegateEvents('textarea[maxlength]', {keyup: true}, this, settings);
  353. });
  354. }
  355. },
  356. /**
  357. * Event handler for the form submit event.
  358. * When settings.submit is enabled:
  359. * - prevents submission if any invalid fields are found.
  360. * - Optionally validates all fields.
  361. * - Optionally moves focus to the first invalid field.
  362. *
  363. * @param {object} evt The jQuery Event object as from the submit event.
  364. *
  365. * @returns {object} undefined if no validation was done, true if validation passed, false if validation didn't.
  366. */
  367. checkValidityOnSubmitHandler = function(evt) {
  368. var $this,
  369. settings = getInstance.call(this),
  370. allValid;
  371. if(settings.submit !== true) {
  372. return;
  373. }
  374. $this = $(this);
  375. allValid = $this.h5Validate('allValid', { revalidate: settings.validateOnSubmit === true });
  376. if(allValid !== true) {
  377. evt.preventDefault();
  378. if(settings.focusFirstInvalidElementOnSubmit === true){
  379. var $invalid = $(settings.allValidSelectors, $this)
  380. .filter(function(index){
  381. return $(this).h5Validate('isValid', { revalidate: false }) !== true;
  382. });
  383. $invalid.first().focus();
  384. }
  385. }
  386. return allValid;
  387. },
  388. instances = [],
  389. buildSettings = function buildSettings(options) {
  390. // Combine defaults and options to get current settings.
  391. var settings = $.extend({}, defaults, options, methods),
  392. activeClass = settings.classPrefix + settings.activeClass;
  393. return $.extend(settings, {
  394. activeClass: activeClass,
  395. activeClassSelector: '.' + activeClass,
  396. requiredClass: settings.classPrefix + settings.requiredClass,
  397. el: this
  398. });
  399. },
  400. getInstance = function getInstance() {
  401. var $parent = $(this).closest('[data-h5-instanceId]');
  402. return instances[$parent.attr('data-h5-instanceId')];
  403. },
  404. setInstance = function setInstance(settings) {
  405. var instanceId = instances.push(settings) - 1;
  406. if (settings.RODom !== true) {
  407. $(this).attr('data-h5-instanceId', instanceId);
  408. }
  409. $(this).trigger('instance', { 'data-h5-instanceId': instanceId });
  410. };
  411. $.h5Validate = {
  412. /**
  413. * Take a map of pattern names and HTML5-compatible regular
  414. * expressions, and add them to the patternLibrary. Patterns in
  415. * the library are automatically assigned to HTML element pattern
  416. * attributes for validation.
  417. *
  418. * @param {Object} patterns A map of pattern names and HTML5 compatible
  419. * regular expressions.
  420. *
  421. * @returns {Object} patternLibrary The modified pattern library
  422. */
  423. addPatterns: function (patterns) {
  424. var patternLibrary = defaults.patternLibrary,
  425. key;
  426. for (key in patterns) {
  427. if (patterns.hasOwnProperty(key)) {
  428. patternLibrary[key] = patterns[key];
  429. }
  430. }
  431. return patternLibrary;
  432. },
  433. /**
  434. * Take a valid jQuery selector, and a list of valid values to
  435. * validate against.
  436. * If the user input isn't in the list, validation fails.
  437. *
  438. * @param {String} selector Any valid jQuery selector.
  439. *
  440. * @param {Array} values A list of valid values to validate selected
  441. * fields against.
  442. */
  443. validValues: function (selector, values) {
  444. var i = 0,
  445. ln = values.length,
  446. pattern = '',
  447. re;
  448. // Build regex pattern
  449. for (i = 0; i < ln; i += 1) {
  450. pattern = pattern ? pattern + '|' + values[i] : values[i];
  451. }
  452. re = new RegExp('^(?:' + pattern + ')$');
  453. $(selector).data('regex', re);
  454. }
  455. };
  456. $.fn.h5Validate = function h5Validate(options) {
  457. var action,
  458. args,
  459. settings;
  460. if (typeof options === 'string' && typeof methods[options] === 'function') {
  461. // Whoah, hold on there! First we need to get the instance:
  462. settings = getInstance.call(this);
  463. args = [].slice.call(arguments, 0);
  464. action = options;
  465. args.shift();
  466. args = $.merge([settings], args);
  467. // Use settings here so we can plug methods into the instance dynamically?
  468. return settings[action].apply(this, args);
  469. }
  470. settings = buildSettings.call(this, options);
  471. setInstance.call(this, settings);
  472. // Returning the jQuery object allows for method chaining.
  473. return methods.bindDelegation.call(this, settings);
  474. };
  475. }(jQuery));