You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1706 lines
55 KiB

9 years ago
6 years ago
10 years ago
10 years ago
6 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
7 years ago
10 years ago
9 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
8 years ago
7 years ago
7 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
6 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
7 years ago
10 years ago
10 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
10 years ago
10 years ago
7 years ago
9 years ago
9 years ago
6 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
10 years ago
7 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
7 years ago
9 years ago
7 years ago
10 years ago
10 years ago
10 years ago
7 years ago
10 years ago
7 years ago
10 years ago
10 years ago
9 years ago
10 years ago
7 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
7 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
7 years ago
7 years ago
7 years ago
10 years ago
8 years ago
10 years ago
9 years ago
9 years ago
8 years ago
10 years ago
10 years ago
9 years ago
10 years ago
7 years ago
10 years ago
7 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
7 years ago
9 years ago
7 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
10 years ago
10 years ago
9 years ago
8 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
9 years ago
  1. /*!
  2. * # Semantic UI 2.4.2 - Form Validation
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Released under the MIT license
  7. * http://opensource.org/licenses/MIT
  8. *
  9. */
  10. ;(function ($, window, document, undefined) {
  11. 'use strict';
  12. window = (typeof window != 'undefined' && window.Math == Math)
  13. ? window
  14. : (typeof self != 'undefined' && self.Math == Math)
  15. ? self
  16. : Function('return this')()
  17. ;
  18. $.fn.form = function(parameters) {
  19. var
  20. $allModules = $(this),
  21. moduleSelector = $allModules.selector || '',
  22. time = new Date().getTime(),
  23. performance = [],
  24. query = arguments[0],
  25. legacyParameters = arguments[1],
  26. methodInvoked = (typeof query == 'string'),
  27. queryArguments = [].slice.call(arguments, 1),
  28. returnedValue
  29. ;
  30. $allModules
  31. .each(function() {
  32. var
  33. $module = $(this),
  34. element = this,
  35. formErrors = [],
  36. keyHeldDown = false,
  37. // set at run-time
  38. $field,
  39. $group,
  40. $message,
  41. $prompt,
  42. $submit,
  43. $clear,
  44. $reset,
  45. settings,
  46. validation,
  47. metadata,
  48. selector,
  49. className,
  50. regExp,
  51. error,
  52. namespace,
  53. moduleNamespace,
  54. eventNamespace,
  55. instance,
  56. module
  57. ;
  58. module = {
  59. initialize: function() {
  60. // settings grabbed at run time
  61. module.get.settings();
  62. if(methodInvoked) {
  63. if(instance === undefined) {
  64. module.instantiate();
  65. }
  66. module.invoke(query);
  67. }
  68. else {
  69. if(instance !== undefined) {
  70. instance.invoke('destroy');
  71. }
  72. module.verbose('Initializing form validation', $module, settings);
  73. module.bindEvents();
  74. module.set.defaults();
  75. module.instantiate();
  76. }
  77. },
  78. instantiate: function() {
  79. module.verbose('Storing instance of module', module);
  80. instance = module;
  81. $module
  82. .data(moduleNamespace, module)
  83. ;
  84. },
  85. destroy: function() {
  86. module.verbose('Destroying previous module', instance);
  87. module.removeEvents();
  88. $module
  89. .removeData(moduleNamespace)
  90. ;
  91. },
  92. refresh: function() {
  93. module.verbose('Refreshing selector cache');
  94. $field = $module.find(selector.field);
  95. $group = $module.find(selector.group);
  96. $message = $module.find(selector.message);
  97. $prompt = $module.find(selector.prompt);
  98. $submit = $module.find(selector.submit);
  99. $clear = $module.find(selector.clear);
  100. $reset = $module.find(selector.reset);
  101. },
  102. submit: function() {
  103. module.verbose('Submitting form', $module);
  104. $module
  105. .submit()
  106. ;
  107. },
  108. attachEvents: function(selector, action) {
  109. action = action || 'submit';
  110. $(selector)
  111. .on('click' + eventNamespace, function(event) {
  112. module[action]();
  113. event.preventDefault();
  114. })
  115. ;
  116. },
  117. bindEvents: function() {
  118. module.verbose('Attaching form events');
  119. $module
  120. .on('submit' + eventNamespace, module.validate.form)
  121. .on('blur' + eventNamespace, selector.field, module.event.field.blur)
  122. .on('click' + eventNamespace, selector.submit, module.submit)
  123. .on('click' + eventNamespace, selector.reset, module.reset)
  124. .on('click' + eventNamespace, selector.clear, module.clear)
  125. ;
  126. if(settings.keyboardShortcuts) {
  127. $module
  128. .on('keydown' + eventNamespace, selector.field, module.event.field.keydown)
  129. ;
  130. }
  131. $field
  132. .each(function() {
  133. var
  134. $input = $(this),
  135. type = $input.prop('type'),
  136. inputEvent = module.get.changeEvent(type, $input)
  137. ;
  138. $(this)
  139. .on(inputEvent + eventNamespace, module.event.field.change)
  140. ;
  141. })
  142. ;
  143. },
  144. clear: function() {
  145. $field
  146. .each(function () {
  147. var
  148. $field = $(this),
  149. $element = $field.parent(),
  150. $fieldGroup = $field.closest($group),
  151. $prompt = $fieldGroup.find(selector.prompt),
  152. defaultValue = $field.data(metadata.defaultValue) || '',
  153. isCheckbox = $element.is(selector.uiCheckbox),
  154. isDropdown = $element.is(selector.uiDropdown),
  155. isErrored = $fieldGroup.hasClass(className.error)
  156. ;
  157. if(isErrored) {
  158. module.verbose('Resetting error on field', $fieldGroup);
  159. $fieldGroup.removeClass(className.error);
  160. $prompt.remove();
  161. }
  162. if(isDropdown) {
  163. module.verbose('Resetting dropdown value', $element, defaultValue);
  164. $element.dropdown('clear');
  165. }
  166. else if(isCheckbox) {
  167. $field.prop('checked', false);
  168. }
  169. else {
  170. module.verbose('Resetting field value', $field, defaultValue);
  171. $field.val('');
  172. }
  173. })
  174. ;
  175. },
  176. reset: function() {
  177. $field
  178. .each(function () {
  179. var
  180. $field = $(this),
  181. $element = $field.parent(),
  182. $fieldGroup = $field.closest($group),
  183. $prompt = $fieldGroup.find(selector.prompt),
  184. defaultValue = $field.data(metadata.defaultValue),
  185. isCheckbox = $element.is(selector.uiCheckbox),
  186. isDropdown = $element.is(selector.uiDropdown),
  187. isErrored = $fieldGroup.hasClass(className.error)
  188. ;
  189. if(defaultValue === undefined) {
  190. return;
  191. }
  192. if(isErrored) {
  193. module.verbose('Resetting error on field', $fieldGroup);
  194. $fieldGroup.removeClass(className.error);
  195. $prompt.remove();
  196. }
  197. if(isDropdown) {
  198. module.verbose('Resetting dropdown value', $element, defaultValue);
  199. $element.dropdown('restore defaults');
  200. }
  201. else if(isCheckbox) {
  202. module.verbose('Resetting checkbox value', $element, defaultValue);
  203. $field.prop('checked', defaultValue);
  204. }
  205. else {
  206. module.verbose('Resetting field value', $field, defaultValue);
  207. $field.val(defaultValue);
  208. }
  209. })
  210. ;
  211. },
  212. determine: {
  213. isValid: function() {
  214. var
  215. allValid = true
  216. ;
  217. $.each(validation, function(fieldName, field) {
  218. if( !( module.validate.field(field, fieldName, true) ) ) {
  219. allValid = false;
  220. }
  221. });
  222. return allValid;
  223. }
  224. },
  225. is: {
  226. bracketedRule: function(rule) {
  227. return (rule.type && rule.type.match(settings.regExp.bracket));
  228. },
  229. shorthandFields: function(fields) {
  230. var
  231. fieldKeys = Object.keys(fields),
  232. firstRule = fields[fieldKeys[0]]
  233. ;
  234. return module.is.shorthandRules(firstRule);
  235. },
  236. // duck type rule test
  237. shorthandRules: function(rules) {
  238. return (typeof rules == 'string' || $.isArray(rules));
  239. },
  240. empty: function($field) {
  241. if(!$field || $field.length === 0) {
  242. return true;
  243. }
  244. else if($field.is('input[type="checkbox"]')) {
  245. return !$field.is(':checked');
  246. }
  247. else {
  248. return module.is.blank($field);
  249. }
  250. },
  251. blank: function($field) {
  252. return $.trim($field.val()) === '';
  253. },
  254. valid: function(field) {
  255. var
  256. allValid = true
  257. ;
  258. if(field) {
  259. module.verbose('Checking if field is valid', field);
  260. return module.validate.field(validation[field], field, false);
  261. }
  262. else {
  263. module.verbose('Checking if form is valid');
  264. $.each(validation, function(fieldName, field) {
  265. if( !module.is.valid(fieldName) ) {
  266. allValid = false;
  267. }
  268. });
  269. return allValid;
  270. }
  271. }
  272. },
  273. removeEvents: function() {
  274. $module
  275. .off(eventNamespace)
  276. ;
  277. $field
  278. .off(eventNamespace)
  279. ;
  280. $submit
  281. .off(eventNamespace)
  282. ;
  283. $field
  284. .off(eventNamespace)
  285. ;
  286. },
  287. event: {
  288. field: {
  289. keydown: function(event) {
  290. var
  291. $field = $(this),
  292. key = event.which,
  293. isInput = $field.is(selector.input),
  294. isCheckbox = $field.is(selector.checkbox),
  295. isInDropdown = ($field.closest(selector.uiDropdown).length > 0),
  296. keyCode = {
  297. enter : 13,
  298. escape : 27
  299. }
  300. ;
  301. if( key == keyCode.escape) {
  302. module.verbose('Escape key pressed blurring field');
  303. $field
  304. .blur()
  305. ;
  306. }
  307. if(!event.ctrlKey && key == keyCode.enter && isInput && !isInDropdown && !isCheckbox) {
  308. if(!keyHeldDown) {
  309. $field
  310. .one('keyup' + eventNamespace, module.event.field.keyup)
  311. ;
  312. module.submit();
  313. module.debug('Enter pressed on input submitting form');
  314. }
  315. keyHeldDown = true;
  316. }
  317. },
  318. keyup: function() {
  319. keyHeldDown = false;
  320. },
  321. blur: function(event) {
  322. var
  323. $field = $(this),
  324. $fieldGroup = $field.closest($group),
  325. validationRules = module.get.validation($field)
  326. ;
  327. if( $fieldGroup.hasClass(className.error) ) {
  328. module.debug('Revalidating field', $field, validationRules);
  329. if(validationRules) {
  330. module.validate.field( validationRules );
  331. }
  332. }
  333. else if(settings.on == 'blur') {
  334. if(validationRules) {
  335. module.validate.field( validationRules );
  336. }
  337. }
  338. },
  339. change: function(event) {
  340. var
  341. $field = $(this),
  342. $fieldGroup = $field.closest($group),
  343. validationRules = module.get.validation($field)
  344. ;
  345. if(validationRules && (settings.on == 'change' || ( $fieldGroup.hasClass(className.error) && settings.revalidate) )) {
  346. clearTimeout(module.timer);
  347. module.timer = setTimeout(function() {
  348. module.debug('Revalidating field', $field, module.get.validation($field));
  349. module.validate.field( validationRules );
  350. }, settings.delay);
  351. }
  352. }
  353. }
  354. },
  355. get: {
  356. ancillaryValue: function(rule) {
  357. if(!rule.type || (!rule.value && !module.is.bracketedRule(rule))) {
  358. return false;
  359. }
  360. return (rule.value !== undefined)
  361. ? rule.value
  362. : rule.type.match(settings.regExp.bracket)[1] + ''
  363. ;
  364. },
  365. ruleName: function(rule) {
  366. if( module.is.bracketedRule(rule) ) {
  367. return rule.type.replace(rule.type.match(settings.regExp.bracket)[0], '');
  368. }
  369. return rule.type;
  370. },
  371. changeEvent: function(type, $input) {
  372. if(type == 'checkbox' || type == 'radio' || type == 'hidden' || $input.is('select')) {
  373. return 'change';
  374. }
  375. else {
  376. return module.get.inputEvent();
  377. }
  378. },
  379. inputEvent: function() {
  380. return (document.createElement('input').oninput !== undefined)
  381. ? 'input'
  382. : (document.createElement('input').onpropertychange !== undefined)
  383. ? 'propertychange'
  384. : 'keyup'
  385. ;
  386. },
  387. fieldsFromShorthand: function(fields) {
  388. var
  389. fullFields = {}
  390. ;
  391. $.each(fields, function(name, rules) {
  392. if(typeof rules == 'string') {
  393. rules = [rules];
  394. }
  395. fullFields[name] = {
  396. rules: []
  397. };
  398. $.each(rules, function(index, rule) {
  399. fullFields[name].rules.push({ type: rule });
  400. });
  401. });
  402. return fullFields;
  403. },
  404. prompt: function(rule, field) {
  405. var
  406. ruleName = module.get.ruleName(rule),
  407. ancillary = module.get.ancillaryValue(rule),
  408. $field = module.get.field(field.identifier),
  409. value = $field.val(),
  410. prompt = $.isFunction(rule.prompt)
  411. ? rule.prompt(value)
  412. : rule.prompt || settings.prompt[ruleName] || settings.text.unspecifiedRule,
  413. requiresValue = (prompt.search('{value}') !== -1),
  414. requiresName = (prompt.search('{name}') !== -1),
  415. $label,
  416. name
  417. ;
  418. if(requiresValue) {
  419. prompt = prompt.replace('{value}', $field.val());
  420. }
  421. if(requiresName) {
  422. $label = $field.closest(selector.group).find('label').eq(0);
  423. name = ($label.length == 1)
  424. ? $label.text()
  425. : $field.prop('placeholder') || settings.text.unspecifiedField
  426. ;
  427. prompt = prompt.replace('{name}', name);
  428. }
  429. prompt = prompt.replace('{identifier}', field.identifier);
  430. prompt = prompt.replace('{ruleValue}', ancillary);
  431. if(!rule.prompt) {
  432. module.verbose('Using default validation prompt for type', prompt, ruleName);
  433. }
  434. return prompt;
  435. },
  436. settings: function() {
  437. if($.isPlainObject(parameters)) {
  438. var
  439. keys = Object.keys(parameters),
  440. isLegacySettings = (keys.length > 0)
  441. ? (parameters[keys[0]].identifier !== undefined && parameters[keys[0]].rules !== undefined)
  442. : false,
  443. ruleKeys
  444. ;
  445. if(isLegacySettings) {
  446. // 1.x (ducktyped)
  447. settings = $.extend(true, {}, $.fn.form.settings, legacyParameters);
  448. validation = $.extend({}, $.fn.form.settings.defaults, parameters);
  449. module.error(settings.error.oldSyntax, element);
  450. module.verbose('Extending settings from legacy parameters', validation, settings);
  451. }
  452. else {
  453. // 2.x
  454. if(parameters.fields && module.is.shorthandFields(parameters.fields)) {
  455. parameters.fields = module.get.fieldsFromShorthand(parameters.fields);
  456. }
  457. settings = $.extend(true, {}, $.fn.form.settings, parameters);
  458. validation = $.extend({}, $.fn.form.settings.defaults, settings.fields);
  459. module.verbose('Extending settings', validation, settings);
  460. }
  461. }
  462. else {
  463. settings = $.fn.form.settings;
  464. validation = $.fn.form.settings.defaults;
  465. module.verbose('Using default form validation', validation, settings);
  466. }
  467. // shorthand
  468. namespace = settings.namespace;
  469. metadata = settings.metadata;
  470. selector = settings.selector;
  471. className = settings.className;
  472. regExp = settings.regExp;
  473. error = settings.error;
  474. moduleNamespace = 'module-' + namespace;
  475. eventNamespace = '.' + namespace;
  476. // grab instance
  477. instance = $module.data(moduleNamespace);
  478. // refresh selector cache
  479. module.refresh();
  480. },
  481. field: function(identifier) {
  482. module.verbose('Finding field with identifier', identifier);
  483. identifier = module.escape.string(identifier);
  484. if($field.filter('#' + identifier).length > 0 ) {
  485. return $field.filter('#' + identifier);
  486. }
  487. else if( $field.filter('[name="' + identifier +'"]').length > 0 ) {
  488. return $field.filter('[name="' + identifier +'"]');
  489. }
  490. else if( $field.filter('[name="' + identifier +'[]"]').length > 0 ) {
  491. return $field.filter('[name="' + identifier +'[]"]');
  492. }
  493. else if( $field.filter('[data-' + metadata.validate + '="'+ identifier +'"]').length > 0 ) {
  494. return $field.filter('[data-' + metadata.validate + '="'+ identifier +'"]');
  495. }
  496. return $('<input/>');
  497. },
  498. fields: function(fields) {
  499. var
  500. $fields = $()
  501. ;
  502. $.each(fields, function(index, name) {
  503. $fields = $fields.add( module.get.field(name) );
  504. });
  505. return $fields;
  506. },
  507. validation: function($field) {
  508. var
  509. fieldValidation,
  510. identifier
  511. ;
  512. if(!validation) {
  513. return false;
  514. }
  515. $.each(validation, function(fieldName, field) {
  516. identifier = field.identifier || fieldName;
  517. if( module.get.field(identifier)[0] == $field[0] ) {
  518. field.identifier = identifier;
  519. fieldValidation = field;
  520. }
  521. });
  522. return fieldValidation || false;
  523. },
  524. value: function (field) {
  525. var
  526. fields = [],
  527. results
  528. ;
  529. fields.push(field);
  530. results = module.get.values.call(element, fields);
  531. return results[field];
  532. },
  533. values: function (fields) {
  534. var
  535. $fields = $.isArray(fields)
  536. ? module.get.fields(fields)
  537. : $field,
  538. values = {}
  539. ;
  540. $fields.each(function(index, field) {
  541. var
  542. $field = $(field),
  543. type = $field.prop('type'),
  544. name = $field.prop('name'),
  545. value = $field.val(),
  546. isCheckbox = $field.is(selector.checkbox),
  547. isRadio = $field.is(selector.radio),
  548. isMultiple = (name.indexOf('[]') !== -1),
  549. isChecked = (isCheckbox)
  550. ? $field.is(':checked')
  551. : false
  552. ;
  553. if(name) {
  554. if(isMultiple) {
  555. name = name.replace('[]', '');
  556. if(!values[name]) {
  557. values[name] = [];
  558. }
  559. if(isCheckbox) {
  560. if(isChecked) {
  561. values[name].push(value || true);
  562. }
  563. else {
  564. values[name].push(false);
  565. }
  566. }
  567. else {
  568. values[name].push(value);
  569. }
  570. }
  571. else {
  572. if(isRadio) {
  573. if(values[name] === undefined || values[name] == false) {
  574. values[name] = (isChecked)
  575. ? value || true
  576. : false
  577. ;
  578. }
  579. }
  580. else if(isCheckbox) {
  581. if(isChecked) {
  582. values[name] = value || true;
  583. }
  584. else {
  585. values[name] = false;
  586. }
  587. }
  588. else {
  589. values[name] = value;
  590. }
  591. }
  592. }
  593. });
  594. return values;
  595. }
  596. },
  597. has: {
  598. field: function(identifier) {
  599. module.verbose('Checking for existence of a field with identifier', identifier);
  600. identifier = module.escape.string(identifier);
  601. if(typeof identifier !== 'string') {
  602. module.error(error.identifier, identifier);
  603. }
  604. if($field.filter('#' + identifier).length > 0 ) {
  605. return true;
  606. }
  607. else if( $field.filter('[name="' + identifier +'"]').length > 0 ) {
  608. return true;
  609. }
  610. else if( $field.filter('[data-' + metadata.validate + '="'+ identifier +'"]').length > 0 ) {
  611. return true;
  612. }
  613. return false;
  614. }
  615. },
  616. escape: {
  617. string: function(text) {
  618. text = String(text);
  619. return text.replace(regExp.escape, '\\$&');
  620. }
  621. },
  622. add: {
  623. // alias
  624. rule: function(name, rules) {
  625. module.add.field(name, rules);
  626. },
  627. field: function(name, rules) {
  628. var
  629. newValidation = {}
  630. ;
  631. if(module.is.shorthandRules(rules)) {
  632. rules = $.isArray(rules)
  633. ? rules
  634. : [rules]
  635. ;
  636. newValidation[name] = {
  637. rules: []
  638. };
  639. $.each(rules, function(index, rule) {
  640. newValidation[name].rules.push({ type: rule });
  641. });
  642. }
  643. else {
  644. newValidation[name] = rules;
  645. }
  646. validation = $.extend({}, validation, newValidation);
  647. module.debug('Adding rules', newValidation, validation);
  648. },
  649. fields: function(fields) {
  650. var
  651. newValidation
  652. ;
  653. if(fields && module.is.shorthandFields(fields)) {
  654. newValidation = module.get.fieldsFromShorthand(fields);
  655. }
  656. else {
  657. newValidation = fields;
  658. }
  659. validation = $.extend({}, validation, newValidation);
  660. },
  661. prompt: function(identifier, errors) {
  662. var
  663. $field = module.get.field(identifier),
  664. $fieldGroup = $field.closest($group),
  665. $prompt = $fieldGroup.children(selector.prompt),
  666. promptExists = ($prompt.length !== 0)
  667. ;
  668. errors = (typeof errors == 'string')
  669. ? [errors]
  670. : errors
  671. ;
  672. module.verbose('Adding field error state', identifier);
  673. $fieldGroup
  674. .addClass(className.error)
  675. ;
  676. if(settings.inline) {
  677. if(!promptExists) {
  678. $prompt = settings.templates.prompt(errors);
  679. $prompt
  680. .appendTo($fieldGroup)
  681. ;
  682. }
  683. $prompt
  684. .html(errors[0])
  685. ;
  686. if(!promptExists) {
  687. if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported')) {
  688. module.verbose('Displaying error with css transition', settings.transition);
  689. $prompt.transition(settings.transition + ' in', settings.duration);
  690. }
  691. else {
  692. module.verbose('Displaying error with fallback javascript animation');
  693. $prompt
  694. .fadeIn(settings.duration)
  695. ;
  696. }
  697. }
  698. else {
  699. module.verbose('Inline errors are disabled, no inline error added', identifier);
  700. }
  701. }
  702. },
  703. errors: function(errors) {
  704. module.debug('Adding form error messages', errors);
  705. module.set.error();
  706. $message
  707. .html( settings.templates.error(errors) )
  708. ;
  709. }
  710. },
  711. remove: {
  712. rule: function(field, rule) {
  713. var
  714. rules = $.isArray(rule)
  715. ? rule
  716. : [rule]
  717. ;
  718. if(rule == undefined) {
  719. module.debug('Removed all rules');
  720. validation[field].rules = [];
  721. return;
  722. }
  723. if(validation[field] == undefined || !$.isArray(validation[field].rules)) {
  724. return;
  725. }
  726. $.each(validation[field].rules, function(index, rule) {
  727. if(rules.indexOf(rule.type) !== -1) {
  728. module.debug('Removed rule', rule.type);
  729. validation[field].rules.splice(index, 1);
  730. }
  731. });
  732. },
  733. field: function(field) {
  734. var
  735. fields = $.isArray(field)
  736. ? field
  737. : [field]
  738. ;
  739. $.each(fields, function(index, field) {
  740. module.remove.rule(field);
  741. });
  742. },
  743. // alias
  744. rules: function(field, rules) {
  745. if($.isArray(field)) {
  746. $.each(fields, function(index, field) {
  747. module.remove.rule(field, rules);
  748. });
  749. }
  750. else {
  751. module.remove.rule(field, rules);
  752. }
  753. },
  754. fields: function(fields) {
  755. module.remove.field(fields);
  756. },
  757. prompt: function(identifier) {
  758. var
  759. $field = module.get.field(identifier),
  760. $fieldGroup = $field.closest($group),
  761. $prompt = $fieldGroup.children(selector.prompt)
  762. ;
  763. $fieldGroup
  764. .removeClass(className.error)
  765. ;
  766. if(settings.inline && $prompt.is(':visible')) {
  767. module.verbose('Removing prompt for field', identifier);
  768. if(settings.transition && $.fn.transition !== undefined && $module.transition('is supported')) {
  769. $prompt.transition(settings.transition + ' out', settings.duration, function() {
  770. $prompt.remove();
  771. });
  772. }
  773. else {
  774. $prompt
  775. .fadeOut(settings.duration, function(){
  776. $prompt.remove();
  777. })
  778. ;
  779. }
  780. }
  781. }
  782. },
  783. set: {
  784. success: function() {
  785. $module
  786. .removeClass(className.error)
  787. .addClass(className.success)
  788. ;
  789. },
  790. defaults: function () {
  791. $field
  792. .each(function () {
  793. var
  794. $field = $(this),
  795. isCheckbox = ($field.filter(selector.checkbox).length > 0),
  796. value = (isCheckbox)
  797. ? $field.is(':checked')
  798. : $field.val()
  799. ;
  800. $field.data(metadata.defaultValue, value);
  801. })
  802. ;
  803. },
  804. error: function() {
  805. $module
  806. .removeClass(className.success)
  807. .addClass(className.error)
  808. ;
  809. },
  810. value: function (field, value) {
  811. var
  812. fields = {}
  813. ;
  814. fields[field] = value;
  815. return module.set.values.call(element, fields);
  816. },
  817. values: function (fields) {
  818. if($.isEmptyObject(fields)) {
  819. return;
  820. }
  821. $.each(fields, function(key, value) {
  822. var
  823. $field = module.get.field(key),
  824. $element = $field.parent(),
  825. isMultiple = $.isArray(value),
  826. isCheckbox = $element.is(selector.uiCheckbox),
  827. isDropdown = $element.is(selector.uiDropdown),
  828. isRadio = ($field.is(selector.radio) && isCheckbox),
  829. fieldExists = ($field.length > 0),
  830. $multipleField
  831. ;
  832. if(fieldExists) {
  833. if(isMultiple && isCheckbox) {
  834. module.verbose('Selecting multiple', value, $field);
  835. $element.checkbox('uncheck');
  836. $.each(value, function(index, value) {
  837. $multipleField = $field.filter('[value="' + value + '"]');
  838. $element = $multipleField.parent();
  839. if($multipleField.length > 0) {
  840. $element.checkbox('check');
  841. }
  842. });
  843. }
  844. else if(isRadio) {
  845. module.verbose('Selecting radio value', value, $field);
  846. $field.filter('[value="' + value + '"]')
  847. .parent(selector.uiCheckbox)
  848. .checkbox('check')
  849. ;
  850. }
  851. else if(isCheckbox) {
  852. module.verbose('Setting checkbox value', value, $element);
  853. if(value === true) {
  854. $element.checkbox('check');
  855. }
  856. else {
  857. $element.checkbox('uncheck');
  858. }
  859. }
  860. else if(isDropdown) {
  861. module.verbose('Setting dropdown value', value, $element);
  862. $element.dropdown('set selected', value);
  863. }
  864. else {
  865. module.verbose('Setting field value', value, $field);
  866. $field.val(value);
  867. }
  868. }
  869. });
  870. }
  871. },
  872. validate: {
  873. form: function(event, ignoreCallbacks) {
  874. var
  875. values = module.get.values(),
  876. apiRequest
  877. ;
  878. // input keydown event will fire submit repeatedly by browser default
  879. if(keyHeldDown) {
  880. return false;
  881. }
  882. // reset errors
  883. formErrors = [];
  884. if( module.determine.isValid() ) {
  885. module.debug('Form has no validation errors, submitting');
  886. module.set.success();
  887. if(ignoreCallbacks !== true) {
  888. return settings.onSuccess.call(element, event, values);
  889. }
  890. }
  891. else {
  892. module.debug('Form has errors');
  893. module.set.error();
  894. if(!settings.inline) {
  895. module.add.errors(formErrors);
  896. }
  897. // prevent ajax submit
  898. if($module.data('moduleApi') !== undefined) {
  899. event.stopImmediatePropagation();
  900. }
  901. if(ignoreCallbacks !== true) {
  902. return settings.onFailure.call(element, formErrors, values);
  903. }
  904. }
  905. },
  906. // takes a validation object and returns whether field passes validation
  907. field: function(field, fieldName, showErrors) {
  908. showErrors = (showErrors !== undefined)
  909. ? showErrors
  910. : true
  911. ;
  912. if(typeof field == 'string') {
  913. module.verbose('Validating field', field);
  914. fieldName = field;
  915. field = validation[field];
  916. }
  917. var
  918. identifier = field.identifier || fieldName,
  919. $field = module.get.field(identifier),
  920. $dependsField = (field.depends)
  921. ? module.get.field(field.depends)
  922. : false,
  923. fieldValid = true,
  924. fieldErrors = []
  925. ;
  926. if(!field.identifier) {
  927. module.debug('Using field name as identifier', identifier);
  928. field.identifier = identifier;
  929. }
  930. if($field.prop('disabled')) {
  931. module.debug('Field is disabled. Skipping', identifier);
  932. fieldValid = true;
  933. }
  934. else if(field.optional && module.is.blank($field)){
  935. module.debug('Field is optional and blank. Skipping', identifier);
  936. fieldValid = true;
  937. }
  938. else if(field.depends && module.is.empty($dependsField)) {
  939. module.debug('Field depends on another value that is not present or empty. Skipping', $dependsField);
  940. fieldValid = true;
  941. }
  942. else if(field.rules !== undefined) {
  943. $.each(field.rules, function(index, rule) {
  944. if( module.has.field(identifier) && !( module.validate.rule(field, rule) ) ) {
  945. module.debug('Field is invalid', identifier, rule.type);
  946. fieldErrors.push(module.get.prompt(rule, field));
  947. fieldValid = false;
  948. }
  949. });
  950. }
  951. if(fieldValid) {
  952. if(showErrors) {
  953. module.remove.prompt(identifier, fieldErrors);
  954. settings.onValid.call($field);
  955. }
  956. }
  957. else {
  958. if(showErrors) {
  959. formErrors = formErrors.concat(fieldErrors);
  960. module.add.prompt(identifier, fieldErrors);
  961. settings.onInvalid.call($field, fieldErrors);
  962. }
  963. return false;
  964. }
  965. return true;
  966. },
  967. // takes validation rule and returns whether field passes rule
  968. rule: function(field, rule) {
  969. var
  970. $field = module.get.field(field.identifier),
  971. type = rule.type,
  972. value = $field.val(),
  973. isValid = true,
  974. ancillary = module.get.ancillaryValue(rule),
  975. ruleName = module.get.ruleName(rule),
  976. ruleFunction = settings.rules[ruleName]
  977. ;
  978. if( !$.isFunction(ruleFunction) ) {
  979. module.error(error.noRule, ruleName);
  980. return;
  981. }
  982. // cast to string avoiding encoding special values
  983. value = (value === undefined || value === '' || value === null)
  984. ? ''
  985. : $.trim(value + '')
  986. ;
  987. return ruleFunction.call($field, value, ancillary);
  988. }
  989. },
  990. setting: function(name, value) {
  991. if( $.isPlainObject(name) ) {
  992. $.extend(true, settings, name);
  993. }
  994. else if(value !== undefined) {
  995. settings[name] = value;
  996. }
  997. else {
  998. return settings[name];
  999. }
  1000. },
  1001. internal: function(name, value) {
  1002. if( $.isPlainObject(name) ) {
  1003. $.extend(true, module, name);
  1004. }
  1005. else if(value !== undefined) {
  1006. module[name] = value;
  1007. }
  1008. else {
  1009. return module[name];
  1010. }
  1011. },
  1012. debug: function() {
  1013. if(!settings.silent && settings.debug) {
  1014. if(settings.performance) {
  1015. module.performance.log(arguments);
  1016. }
  1017. else {
  1018. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  1019. module.debug.apply(console, arguments);
  1020. }
  1021. }
  1022. },
  1023. verbose: function() {
  1024. if(!settings.silent && settings.verbose && settings.debug) {
  1025. if(settings.performance) {
  1026. module.performance.log(arguments);
  1027. }
  1028. else {
  1029. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  1030. module.verbose.apply(console, arguments);
  1031. }
  1032. }
  1033. },
  1034. error: function() {
  1035. if(!settings.silent) {
  1036. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  1037. module.error.apply(console, arguments);
  1038. }
  1039. },
  1040. performance: {
  1041. log: function(message) {
  1042. var
  1043. currentTime,
  1044. executionTime,
  1045. previousTime
  1046. ;
  1047. if(settings.performance) {
  1048. currentTime = new Date().getTime();
  1049. previousTime = time || currentTime;
  1050. executionTime = currentTime - previousTime;
  1051. time = currentTime;
  1052. performance.push({
  1053. 'Name' : message[0],
  1054. 'Arguments' : [].slice.call(message, 1) || '',
  1055. 'Element' : element,
  1056. 'Execution Time' : executionTime
  1057. });
  1058. }
  1059. clearTimeout(module.performance.timer);
  1060. module.performance.timer = setTimeout(module.performance.display, 500);
  1061. },
  1062. display: function() {
  1063. var
  1064. title = settings.name + ':',
  1065. totalTime = 0
  1066. ;
  1067. time = false;
  1068. clearTimeout(module.performance.timer);
  1069. $.each(performance, function(index, data) {
  1070. totalTime += data['Execution Time'];
  1071. });
  1072. title += ' ' + totalTime + 'ms';
  1073. if(moduleSelector) {
  1074. title += ' \'' + moduleSelector + '\'';
  1075. }
  1076. if($allModules.length > 1) {
  1077. title += ' ' + '(' + $allModules.length + ')';
  1078. }
  1079. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  1080. console.groupCollapsed(title);
  1081. if(console.table) {
  1082. console.table(performance);
  1083. }
  1084. else {
  1085. $.each(performance, function(index, data) {
  1086. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  1087. });
  1088. }
  1089. console.groupEnd();
  1090. }
  1091. performance = [];
  1092. }
  1093. },
  1094. invoke: function(query, passedArguments, context) {
  1095. var
  1096. object = instance,
  1097. maxDepth,
  1098. found,
  1099. response
  1100. ;
  1101. passedArguments = passedArguments || queryArguments;
  1102. context = element || context;
  1103. if(typeof query == 'string' && object !== undefined) {
  1104. query = query.split(/[\. ]/);
  1105. maxDepth = query.length - 1;
  1106. $.each(query, function(depth, value) {
  1107. var camelCaseValue = (depth != maxDepth)
  1108. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  1109. : query
  1110. ;
  1111. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  1112. object = object[camelCaseValue];
  1113. }
  1114. else if( object[camelCaseValue] !== undefined ) {
  1115. found = object[camelCaseValue];
  1116. return false;
  1117. }
  1118. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  1119. object = object[value];
  1120. }
  1121. else if( object[value] !== undefined ) {
  1122. found = object[value];
  1123. return false;
  1124. }
  1125. else {
  1126. return false;
  1127. }
  1128. });
  1129. }
  1130. if( $.isFunction( found ) ) {
  1131. response = found.apply(context, passedArguments);
  1132. }
  1133. else if(found !== undefined) {
  1134. response = found;
  1135. }
  1136. if($.isArray(returnedValue)) {
  1137. returnedValue.push(response);
  1138. }
  1139. else if(returnedValue !== undefined) {
  1140. returnedValue = [returnedValue, response];
  1141. }
  1142. else if(response !== undefined) {
  1143. returnedValue = response;
  1144. }
  1145. return found;
  1146. }
  1147. };
  1148. module.initialize();
  1149. })
  1150. ;
  1151. return (returnedValue !== undefined)
  1152. ? returnedValue
  1153. : this
  1154. ;
  1155. };
  1156. $.fn.form.settings = {
  1157. name : 'Form',
  1158. namespace : 'form',
  1159. debug : false,
  1160. verbose : false,
  1161. performance : true,
  1162. fields : false,
  1163. keyboardShortcuts : true,
  1164. on : 'submit',
  1165. inline : false,
  1166. delay : 200,
  1167. revalidate : true,
  1168. transition : 'scale',
  1169. duration : 200,
  1170. onValid : function() {},
  1171. onInvalid : function() {},
  1172. onSuccess : function() { return true; },
  1173. onFailure : function() { return false; },
  1174. metadata : {
  1175. defaultValue : 'default',
  1176. validate : 'validate'
  1177. },
  1178. regExp: {
  1179. htmlID : /^[a-zA-Z][\w:.-]*$/g,
  1180. bracket : /\[(.*)\]/i,
  1181. decimal : /^\d+\.?\d*$/,
  1182. email : /^[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$/i,
  1183. escape : /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,
  1184. flags : /^\/(.*)\/(.*)?/,
  1185. integer : /^\-?\d+$/,
  1186. number : /^\-?\d*(\.\d+)?$/,
  1187. url : /(https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})/i
  1188. },
  1189. text: {
  1190. unspecifiedRule : 'Please enter a valid value',
  1191. unspecifiedField : 'This field'
  1192. },
  1193. prompt: {
  1194. empty : '{name} must have a value',
  1195. checked : '{name} must be checked',
  1196. email : '{name} must be a valid e-mail',
  1197. url : '{name} must be a valid url',
  1198. regExp : '{name} is not formatted correctly',
  1199. integer : '{name} must be an integer',
  1200. decimal : '{name} must be a decimal number',
  1201. number : '{name} must be set to a number',
  1202. is : '{name} must be "{ruleValue}"',
  1203. isExactly : '{name} must be exactly "{ruleValue}"',
  1204. not : '{name} cannot be set to "{ruleValue}"',
  1205. notExactly : '{name} cannot be set to exactly "{ruleValue}"',
  1206. contain : '{name} must contain "{ruleValue}"',
  1207. containExactly : '{name} must contain exactly "{ruleValue}"',
  1208. doesntContain : '{name} cannot contain "{ruleValue}"',
  1209. doesntContainExactly : '{name} cannot contain exactly "{ruleValue}"',
  1210. minLength : '{name} must be at least {ruleValue} characters',
  1211. length : '{name} must be at least {ruleValue} characters',
  1212. exactLength : '{name} must be exactly {ruleValue} characters',
  1213. maxLength : '{name} cannot be longer than {ruleValue} characters',
  1214. match : '{name} must match {ruleValue} field',
  1215. different : '{name} must have a different value than {ruleValue} field',
  1216. creditCard : '{name} must be a valid credit card number',
  1217. minCount : '{name} must have at least {ruleValue} choices',
  1218. exactCount : '{name} must have exactly {ruleValue} choices',
  1219. maxCount : '{name} must have {ruleValue} or less choices'
  1220. },
  1221. selector : {
  1222. checkbox : 'input[type="checkbox"], input[type="radio"]',
  1223. clear : '.clear',
  1224. field : 'input, textarea, select',
  1225. group : '.field',
  1226. input : 'input',
  1227. message : '.error.message',
  1228. prompt : '.prompt.label',
  1229. radio : 'input[type="radio"]',
  1230. reset : '.reset:not([type="reset"])',
  1231. submit : '.submit:not([type="submit"])',
  1232. uiCheckbox : '.ui.checkbox',
  1233. uiDropdown : '.ui.dropdown'
  1234. },
  1235. className : {
  1236. error : 'error',
  1237. label : 'ui prompt label',
  1238. pressed : 'down',
  1239. success : 'success'
  1240. },
  1241. error: {
  1242. identifier : 'You must specify a string identifier for each field',
  1243. method : 'The method you called is not defined.',
  1244. noRule : 'There is no rule matching the one you specified',
  1245. oldSyntax : 'Starting in 2.0 forms now only take a single settings object. Validation settings converted to new syntax automatically.'
  1246. },
  1247. templates: {
  1248. // template that produces error message
  1249. error: function(errors) {
  1250. var
  1251. html = '<ul class="list">'
  1252. ;
  1253. $.each(errors, function(index, value) {
  1254. html += '<li>' + value + '</li>';
  1255. });
  1256. html += '</ul>';
  1257. return $(html);
  1258. },
  1259. // template that produces label
  1260. prompt: function(errors) {
  1261. return $('<div/>')
  1262. .addClass('ui basic red pointing prompt label')
  1263. .html(errors[0])
  1264. ;
  1265. }
  1266. },
  1267. rules: {
  1268. // is not empty or blank string
  1269. empty: function(value) {
  1270. return !(value === undefined || '' === value || $.isArray(value) && value.length === 0);
  1271. },
  1272. // checkbox checked
  1273. checked: function() {
  1274. return ($(this).filter(':checked').length > 0);
  1275. },
  1276. // is most likely an email
  1277. email: function(value){
  1278. return $.fn.form.settings.regExp.email.test(value);
  1279. },
  1280. // value is most likely url
  1281. url: function(value) {
  1282. return $.fn.form.settings.regExp.url.test(value);
  1283. },
  1284. // matches specified regExp
  1285. regExp: function(value, regExp) {
  1286. if(regExp instanceof RegExp) {
  1287. return value.match(regExp);
  1288. }
  1289. var
  1290. regExpParts = regExp.match($.fn.form.settings.regExp.flags),
  1291. flags
  1292. ;
  1293. // regular expression specified as /baz/gi (flags)
  1294. if(regExpParts) {
  1295. regExp = (regExpParts.length >= 2)
  1296. ? regExpParts[1]
  1297. : regExp
  1298. ;
  1299. flags = (regExpParts.length >= 3)
  1300. ? regExpParts[2]
  1301. : ''
  1302. ;
  1303. }
  1304. return value.match( new RegExp(regExp, flags) );
  1305. },
  1306. // is valid integer or matches range
  1307. integer: function(value, range) {
  1308. var
  1309. intRegExp = $.fn.form.settings.regExp.integer,
  1310. min,
  1311. max,
  1312. parts
  1313. ;
  1314. if( !range || ['', '..'].indexOf(range) !== -1) {
  1315. // do nothing
  1316. }
  1317. else if(range.indexOf('..') == -1) {
  1318. if(intRegExp.test(range)) {
  1319. min = max = range - 0;
  1320. }
  1321. }
  1322. else {
  1323. parts = range.split('..', 2);
  1324. if(intRegExp.test(parts[0])) {
  1325. min = parts[0] - 0;
  1326. }
  1327. if(intRegExp.test(parts[1])) {
  1328. max = parts[1] - 0;
  1329. }
  1330. }
  1331. return (
  1332. intRegExp.test(value) &&
  1333. (min === undefined || value >= min) &&
  1334. (max === undefined || value <= max)
  1335. );
  1336. },
  1337. // is valid number (with decimal)
  1338. decimal: function(value) {
  1339. return $.fn.form.settings.regExp.decimal.test(value);
  1340. },
  1341. // is valid number
  1342. number: function(value) {
  1343. return $.fn.form.settings.regExp.number.test(value);
  1344. },
  1345. // is value (case insensitive)
  1346. is: function(value, text) {
  1347. text = (typeof text == 'string')
  1348. ? text.toLowerCase()
  1349. : text
  1350. ;
  1351. value = (typeof value == 'string')
  1352. ? value.toLowerCase()
  1353. : value
  1354. ;
  1355. return (value == text);
  1356. },
  1357. // is value
  1358. isExactly: function(value, text) {
  1359. return (value == text);
  1360. },
  1361. // value is not another value (case insensitive)
  1362. not: function(value, notValue) {
  1363. value = (typeof value == 'string')
  1364. ? value.toLowerCase()
  1365. : value
  1366. ;
  1367. notValue = (typeof notValue == 'string')
  1368. ? notValue.toLowerCase()
  1369. : notValue
  1370. ;
  1371. return (value != notValue);
  1372. },
  1373. // value is not another value (case sensitive)
  1374. notExactly: function(value, notValue) {
  1375. return (value != notValue);
  1376. },
  1377. // value contains text (insensitive)
  1378. contains: function(value, text) {
  1379. // escape regex characters
  1380. text = text.replace($.fn.form.settings.regExp.escape, "\\$&");
  1381. return (value.search( new RegExp(text, 'i') ) !== -1);
  1382. },
  1383. // value contains text (case sensitive)
  1384. containsExactly: function(value, text) {
  1385. // escape regex characters
  1386. text = text.replace($.fn.form.settings.regExp.escape, "\\$&");
  1387. return (value.search( new RegExp(text) ) !== -1);
  1388. },
  1389. // value contains text (insensitive)
  1390. doesntContain: function(value, text) {
  1391. // escape regex characters
  1392. text = text.replace($.fn.form.settings.regExp.escape, "\\$&");
  1393. return (value.search( new RegExp(text, 'i') ) === -1);
  1394. },
  1395. // value contains text (case sensitive)
  1396. doesntContainExactly: function(value, text) {
  1397. // escape regex characters
  1398. text = text.replace($.fn.form.settings.regExp.escape, "\\$&");
  1399. return (value.search( new RegExp(text) ) === -1);
  1400. },
  1401. // is at least string length
  1402. minLength: function(value, requiredLength) {
  1403. return (value !== undefined)
  1404. ? (value.length >= requiredLength)
  1405. : false
  1406. ;
  1407. },
  1408. // see rls notes for 2.0.6 (this is a duplicate of minLength)
  1409. length: function(value, requiredLength) {
  1410. return (value !== undefined)
  1411. ? (value.length >= requiredLength)
  1412. : false
  1413. ;
  1414. },
  1415. // is exactly length
  1416. exactLength: function(value, requiredLength) {
  1417. return (value !== undefined)
  1418. ? (value.length == requiredLength)
  1419. : false
  1420. ;
  1421. },
  1422. // is less than length
  1423. maxLength: function(value, maxLength) {
  1424. return (value !== undefined)
  1425. ? (value.length <= maxLength)
  1426. : false
  1427. ;
  1428. },
  1429. // matches another field
  1430. match: function(value, identifier) {
  1431. var
  1432. $form = $(this),
  1433. matchingValue
  1434. ;
  1435. if( $('[data-validate="'+ identifier +'"]').length > 0 ) {
  1436. matchingValue = $('[data-validate="'+ identifier +'"]').val();
  1437. }
  1438. else if($('#' + identifier).length > 0) {
  1439. matchingValue = $('#' + identifier).val();
  1440. }
  1441. else if($('[name="' + identifier +'"]').length > 0) {
  1442. matchingValue = $('[name="' + identifier + '"]').val();
  1443. }
  1444. else if( $('[name="' + identifier +'[]"]').length > 0 ) {
  1445. matchingValue = $('[name="' + identifier +'[]"]');
  1446. }
  1447. return (matchingValue !== undefined)
  1448. ? ( value.toString() == matchingValue.toString() )
  1449. : false
  1450. ;
  1451. },
  1452. // different than another field
  1453. different: function(value, identifier) {
  1454. // use either id or name of field
  1455. var
  1456. $form = $(this),
  1457. matchingValue
  1458. ;
  1459. if( $('[data-validate="'+ identifier +'"]').length > 0 ) {
  1460. matchingValue = $('[data-validate="'+ identifier +'"]').val();
  1461. }
  1462. else if($('#' + identifier).length > 0) {
  1463. matchingValue = $('#' + identifier).val();
  1464. }
  1465. else if($('[name="' + identifier +'"]').length > 0) {
  1466. matchingValue = $('[name="' + identifier + '"]').val();
  1467. }
  1468. else if( $('[name="' + identifier +'[]"]').length > 0 ) {
  1469. matchingValue = $('[name="' + identifier +'[]"]');
  1470. }
  1471. return (matchingValue !== undefined)
  1472. ? ( value.toString() !== matchingValue.toString() )
  1473. : false
  1474. ;
  1475. },
  1476. creditCard: function(cardNumber, cardTypes) {
  1477. var
  1478. cards = {
  1479. visa: {
  1480. pattern : /^4/,
  1481. length : [16]
  1482. },
  1483. amex: {
  1484. pattern : /^3[47]/,
  1485. length : [15]
  1486. },
  1487. mastercard: {
  1488. pattern : /^5[1-5]/,
  1489. length : [16]
  1490. },
  1491. discover: {
  1492. pattern : /^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)/,
  1493. length : [16]
  1494. },
  1495. unionPay: {
  1496. pattern : /^(62|88)/,
  1497. length : [16, 17, 18, 19]
  1498. },
  1499. jcb: {
  1500. pattern : /^35(2[89]|[3-8][0-9])/,
  1501. length : [16]
  1502. },
  1503. maestro: {
  1504. pattern : /^(5018|5020|5038|6304|6759|676[1-3])/,
  1505. length : [12, 13, 14, 15, 16, 17, 18, 19]
  1506. },
  1507. dinersClub: {
  1508. pattern : /^(30[0-5]|^36)/,
  1509. length : [14]
  1510. },
  1511. laser: {
  1512. pattern : /^(6304|670[69]|6771)/,
  1513. length : [16, 17, 18, 19]
  1514. },
  1515. visaElectron: {
  1516. pattern : /^(4026|417500|4508|4844|491(3|7))/,
  1517. length : [16]
  1518. }
  1519. },
  1520. valid = {},
  1521. validCard = false,
  1522. requiredTypes = (typeof cardTypes == 'string')
  1523. ? cardTypes.split(',')
  1524. : false,
  1525. unionPay,
  1526. validation
  1527. ;
  1528. if(typeof cardNumber !== 'string' || cardNumber.length === 0) {
  1529. return;
  1530. }
  1531. // allow dashes in card
  1532. cardNumber = cardNumber.replace(/[\-]/g, '');
  1533. // verify card types
  1534. if(requiredTypes) {
  1535. $.each(requiredTypes, function(index, type){
  1536. // verify each card type
  1537. validation = cards[type];
  1538. if(validation) {
  1539. valid = {
  1540. length : ($.inArray(cardNumber.length, validation.length) !== -1),
  1541. pattern : (cardNumber.search(validation.pattern) !== -1)
  1542. };
  1543. if(valid.length && valid.pattern) {
  1544. validCard = true;
  1545. }
  1546. }
  1547. });
  1548. if(!validCard) {
  1549. return false;
  1550. }
  1551. }
  1552. // skip luhn for UnionPay
  1553. unionPay = {
  1554. number : ($.inArray(cardNumber.length, cards.unionPay.length) !== -1),
  1555. pattern : (cardNumber.search(cards.unionPay.pattern) !== -1)
  1556. };
  1557. if(unionPay.number && unionPay.pattern) {
  1558. return true;
  1559. }
  1560. // verify luhn, adapted from <https://gist.github.com/2134376>
  1561. var
  1562. length = cardNumber.length,
  1563. multiple = 0,
  1564. producedValue = [
  1565. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
  1566. [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
  1567. ],
  1568. sum = 0
  1569. ;
  1570. while (length--) {
  1571. sum += producedValue[multiple][parseInt(cardNumber.charAt(length), 10)];
  1572. multiple ^= 1;
  1573. }
  1574. return (sum % 10 === 0 && sum > 0);
  1575. },
  1576. minCount: function(value, minCount) {
  1577. if(minCount == 0) {
  1578. return true;
  1579. }
  1580. if(minCount == 1) {
  1581. return (value !== '');
  1582. }
  1583. return (value.split(',').length >= minCount);
  1584. },
  1585. exactCount: function(value, exactCount) {
  1586. if(exactCount == 0) {
  1587. return (value === '');
  1588. }
  1589. if(exactCount == 1) {
  1590. return (value !== '' && value.search(',') === -1);
  1591. }
  1592. return (value.split(',').length == exactCount);
  1593. },
  1594. maxCount: function(value, maxCount) {
  1595. if(maxCount == 0) {
  1596. return false;
  1597. }
  1598. if(maxCount == 1) {
  1599. return (value.search(',') === -1);
  1600. }
  1601. return (value.split(',').length <= maxCount);
  1602. }
  1603. }
  1604. };
  1605. })( jQuery, window, document );