validation.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/forms/validation", ["jquery", "Site"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(require("jquery"), require("Site"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(global.jQuery, global.Site);
  11. global.formsValidation = mod.exports;
  12. }
  13. })(this, function (_jquery, _Site) {
  14. "use strict";
  15. _jquery = babelHelpers.interopRequireDefault(_jquery);
  16. (0, _jquery.default)(document).ready(function ($$$1) {
  17. (0, _Site.run)();
  18. }); // Example Validataion Full
  19. // ------------------------
  20. (function () {
  21. (0, _jquery.default)('#exampleFullForm').formValidation({
  22. framework: "bootstrap4",
  23. button: {
  24. selector: '#validateButton1',
  25. disabled: 'disabled'
  26. },
  27. icon: null,
  28. fields: {
  29. username: {
  30. validators: {
  31. notEmpty: {
  32. message: 'The username is required'
  33. },
  34. stringLength: {
  35. min: 6,
  36. max: 30
  37. },
  38. regexp: {
  39. regexp: /^[a-zA-Z0-9]+$/
  40. }
  41. }
  42. },
  43. email: {
  44. validators: {
  45. notEmpty: {
  46. message: 'The username is required'
  47. },
  48. emailAddress: {
  49. message: 'The email address is not valid'
  50. }
  51. }
  52. },
  53. password: {
  54. validators: {
  55. notEmpty: {
  56. message: 'The password is required'
  57. },
  58. stringLength: {
  59. min: 8
  60. }
  61. }
  62. },
  63. birthday: {
  64. validators: {
  65. notEmpty: {
  66. message: 'The birthday is required'
  67. },
  68. date: {
  69. format: 'YYYY/MM/DD'
  70. }
  71. }
  72. },
  73. github: {
  74. validators: {
  75. url: {
  76. message: 'The url is not valid'
  77. }
  78. }
  79. },
  80. skills: {
  81. validators: {
  82. notEmpty: {
  83. message: 'The skills is required'
  84. },
  85. stringLength: {
  86. max: 300
  87. }
  88. }
  89. },
  90. porto_is: {
  91. validators: {
  92. notEmpty: {
  93. message: 'Please specify at least one'
  94. }
  95. }
  96. },
  97. 'for[]': {
  98. validators: {
  99. notEmpty: {
  100. message: 'Please specify at least one'
  101. }
  102. }
  103. },
  104. company: {
  105. validators: {
  106. notEmpty: {
  107. message: 'Please company'
  108. }
  109. }
  110. },
  111. browsers: {
  112. validators: {
  113. notEmpty: {
  114. message: 'Please specify at least one browser you use daily for development'
  115. }
  116. }
  117. }
  118. },
  119. err: {
  120. clazz: 'invalid-feedback'
  121. },
  122. control: {
  123. // The CSS class for valid control
  124. valid: 'is-valid',
  125. // The CSS class for invalid control
  126. invalid: 'is-invalid'
  127. },
  128. row: {
  129. invalid: 'has-danger'
  130. }
  131. });
  132. })(); // Example Validataion Constraints
  133. // -------------------------------
  134. (function () {
  135. (0, _jquery.default)('#exampleConstraintsForm, #exampleConstraintsFormTypes').formValidation({
  136. framework: "bootstrap4",
  137. icon: null,
  138. fields: {
  139. type_email: {
  140. validators: {
  141. emailAddress: {
  142. message: 'The email address is not valid'
  143. }
  144. }
  145. },
  146. type_url: {
  147. validators: {
  148. url: {
  149. message: 'The url is not valid'
  150. }
  151. }
  152. },
  153. type_digits: {
  154. validators: {
  155. digits: {
  156. message: 'The value is not digits'
  157. }
  158. }
  159. },
  160. type_numberic: {
  161. validators: {
  162. integer: {
  163. message: 'The value is not an number'
  164. }
  165. }
  166. },
  167. type_phone: {
  168. validators: {
  169. phone: {
  170. message: 'The value is not an phone(US)'
  171. }
  172. }
  173. },
  174. type_credit_card: {
  175. validators: {
  176. creditCard: {
  177. message: 'The credit card number is not valid'
  178. }
  179. }
  180. },
  181. type_date: {
  182. validators: {
  183. date: {
  184. format: 'YYYY/MM/DD'
  185. }
  186. }
  187. },
  188. type_color: {
  189. validators: {
  190. color: {
  191. type: ['hex', 'hsl', 'hsla', 'keyword', 'rgb', 'rgba'],
  192. // The default value for type
  193. message: 'The value is not valid color'
  194. }
  195. }
  196. },
  197. type_ip: {
  198. validators: {
  199. ip: {
  200. ipv4: true,
  201. ipv6: true,
  202. message: 'The value is not valid ip(v4 or v6)'
  203. }
  204. }
  205. }
  206. },
  207. err: {
  208. clazz: 'invalid-feedback'
  209. },
  210. control: {
  211. // The CSS class for valid control
  212. valid: 'is-valid',
  213. // The CSS class for invalid control
  214. invalid: 'is-invalid'
  215. },
  216. row: {
  217. invalid: 'has-danger'
  218. }
  219. });
  220. })(); // Example Validataion Standard Mode
  221. // ---------------------------------
  222. (function () {
  223. (0, _jquery.default)('#exampleStandardForm').formValidation({
  224. framework: "bootstrap4",
  225. button: {
  226. selector: '#validateButton2',
  227. disabled: 'disabled'
  228. },
  229. icon: null,
  230. fields: {
  231. standard_fullName: {
  232. validators: {
  233. notEmpty: {
  234. message: 'The full name is required and cannot be empty'
  235. }
  236. }
  237. },
  238. standard_email: {
  239. validators: {
  240. notEmpty: {
  241. message: 'The email address is required and cannot be empty'
  242. },
  243. emailAddress: {
  244. message: 'The email address is not valid'
  245. }
  246. }
  247. },
  248. standard_content: {
  249. validators: {
  250. notEmpty: {
  251. message: 'The content is required and cannot be empty'
  252. },
  253. stringLength: {
  254. max: 500,
  255. message: 'The content must be less than 500 characters long'
  256. }
  257. }
  258. }
  259. },
  260. err: {
  261. clazz: 'invalid-feedback'
  262. },
  263. control: {
  264. // The CSS class for valid control
  265. valid: 'is-valid',
  266. // The CSS class for invalid control
  267. invalid: 'is-invalid'
  268. },
  269. row: {
  270. invalid: 'has-danger'
  271. }
  272. });
  273. })(); // Example Validataion Summary Mode
  274. // -------------------------------
  275. (function () {
  276. (0, _jquery.default)('.summary-errors').hide();
  277. (0, _jquery.default)('#exampleSummaryForm').formValidation({
  278. framework: "bootstrap4",
  279. button: {
  280. selector: '#validateButton3',
  281. disabled: 'disabled'
  282. },
  283. icon: null,
  284. fields: {
  285. summary_fullName: {
  286. validators: {
  287. notEmpty: {
  288. message: 'The full name is required and cannot be empty'
  289. }
  290. }
  291. },
  292. summary_email: {
  293. validators: {
  294. notEmpty: {
  295. message: 'The email address is required and cannot be empty'
  296. },
  297. emailAddress: {
  298. message: 'The email address is not valid'
  299. }
  300. }
  301. },
  302. summary_content: {
  303. validators: {
  304. notEmpty: {
  305. message: 'The content is required and cannot be empty'
  306. },
  307. stringLength: {
  308. max: 500,
  309. message: 'The content must be less than 500 characters long'
  310. }
  311. }
  312. }
  313. },
  314. err: {
  315. clazz: 'invalid-feedback'
  316. },
  317. control: {
  318. // The CSS class for valid control
  319. valid: 'is-valid',
  320. // The CSS class for invalid control
  321. invalid: 'is-invalid'
  322. },
  323. row: {
  324. invalid: 'has-danger'
  325. }
  326. }).on('success.form.fv', function (e) {
  327. // Reset the message element when the form is valid
  328. (0, _jquery.default)('.summary-errors').html('');
  329. }).on('err.field.fv', function (e, data) {
  330. // data.fv --> The FormValidation instance
  331. // data.field --> The field name
  332. // data.element --> The field element
  333. (0, _jquery.default)('.summary-errors').show(); // Get the messages of field
  334. var messages = data.fv.getMessages(data.element); // Remove the field messages if they're already available
  335. (0, _jquery.default)('.summary-errors').find('li[data-field="' + data.field + '"]').remove(); // Loop over the messages
  336. for (var i in messages) {
  337. // Create new 'li' element to show the message
  338. (0, _jquery.default)('<li/>').attr('data-field', data.field).wrapInner((0, _jquery.default)('<a/>').attr('href', 'javascript: void(0);') // .addClass('alert alert-danger alert-dismissible')
  339. .html(messages[i]).on('click', function (e) {
  340. // Focus on the invalid field
  341. data.element.focus();
  342. })).appendTo('.summary-errors > ul');
  343. } // Hide the default message
  344. // $field.data('fv.messages') returns the default element containing the messages
  345. data.element.data('fv.messages').find('.invalid-feedback[data-fv-for="' + data.field + '"]').hide();
  346. }).on('success.field.fv', function (e, data) {
  347. // Remove the field messages
  348. (0, _jquery.default)('.summary-errors > ul').find('li[data-field="' + data.field + '"]').remove();
  349. if ((0, _jquery.default)('#exampleSummaryForm').data('formValidation').isValid()) {
  350. (0, _jquery.default)('.summary-errors').hide();
  351. }
  352. });
  353. })();
  354. });