Dashboard sipadu mbip
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.

form-validation.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. 'use strict';
  2. $(document).ready(function() {
  3. validate.extend(validate.validators.datetime, {
  4. parse: function(value, options) {
  5. return +moment.utc(value);
  6. },
  7. // Input is a unix timestamp
  8. format: function(value, options) {
  9. var format = options.dateOnly ? "DD/MM/YYYY" : "DD/MM/YYYY";
  10. return moment.utc(value).format(format);
  11. }
  12. });
  13. // These are the constraints used to validate the form
  14. var constraints = {
  15. email: {
  16. // Email is required
  17. presence: true,
  18. // and must be an email (duh)
  19. email: true
  20. },
  21. password: {
  22. // Password is also required
  23. presence: true,
  24. // And must be at least 5 characters long
  25. length: {
  26. minimum: 5
  27. }
  28. },
  29. "confirm-password": {
  30. // You need to confirm your password
  31. presence: true,
  32. // and it needs to be equal to the other password
  33. equality: {
  34. attribute: "password",
  35. message: "^The passwords does not match"
  36. }
  37. },
  38. username: {
  39. // You need to pick a username too
  40. presence: true,
  41. // And it must be between 3 and 20 characters long
  42. length: {
  43. minimum: 3,
  44. maximum: 20
  45. },
  46. format: {
  47. // We don't allow anything that a-z and 0-9
  48. pattern: "[a-z0-9]+",
  49. // but we don't care if the username is uppercase or lowercase
  50. flags: "i",
  51. message: "can only contain a-z and 0-9"
  52. }
  53. },
  54. gender: {
  55. // You need to pick a gender too
  56. presence: true,
  57. }
  58. };
  59. // Hook up the form so we can prevent it from being posted
  60. var form = document.querySelector("form#main");
  61. form.addEventListener("submit", function(ev) {
  62. ev.preventDefault();
  63. handleFormSubmit(form);
  64. });
  65. // Hook up the inputs to validate on the fly
  66. var inputs = document.querySelectorAll("input, textarea, select")
  67. for (var i = 0; i < inputs.length; ++i) {
  68. inputs.item(i).addEventListener("change", function(ev) {
  69. var errors = validate(form, constraints) || {};
  70. showErrorsForInput(this, errors[this.name]);
  71. });
  72. }
  73. function handleFormSubmit(form, input) {
  74. // validate the form aainst the constraints
  75. var errors = validate(form, constraints);
  76. // then we update the form to reflect the results
  77. showErrors(form, errors || {});
  78. if (!errors) {
  79. showSuccess();
  80. }
  81. }
  82. // Updates the inputs with the validation errors
  83. function showErrors(form, errors) {
  84. // We loop through all the inputs and show the errors for that input
  85. _.each(form.querySelectorAll("input[name], select[name]"), function(input) {
  86. // Since the errors can be null if no errors were found we need to handle
  87. // that
  88. showErrorsForInput(input, errors && errors[input.name]);
  89. });
  90. }
  91. // Shows the errors for a specific input
  92. function showErrorsForInput(input, errors) {
  93. // This is the root of the input
  94. var formGroup = closestParent(input.parentNode, "md-input-wrapper")
  95. // Find where the error messages will be insert into
  96. ,
  97. messages = formGroup.querySelector(".messages");
  98. // First we remove any old messages and resets the classes
  99. resetFormGroup(formGroup);
  100. // If we have errors
  101. if (errors) {
  102. // we first mark the group has having errors
  103. formGroup.classList.add("has-error");
  104. // then we append all the errors
  105. _.each(errors, function(error) {
  106. addError(messages, error, input);
  107. });
  108. } else {
  109. // otherwise we simply mark it as success
  110. formGroup.classList.add("has-success");
  111. }
  112. }
  113. // Recusively finds the closest parent that has the specified class
  114. function closestParent(child, className) {
  115. if (!child || child == document) {
  116. return null;
  117. }
  118. if (child.classList.contains(className)) {
  119. return child;
  120. } else {
  121. return closestParent(child.parentNode, className);
  122. }
  123. }
  124. function resetFormGroup(formGroup) {
  125. // Remove the success and error classes
  126. formGroup.classList.remove("has-error");
  127. formGroup.classList.remove("has-success");
  128. // and remove any old messages
  129. _.each(formGroup.querySelectorAll(".text-danger"), function(el) {
  130. el.parentNode.removeChild(el);
  131. });
  132. }
  133. // Adds the specified error with the following markup
  134. // <p class="help-block error">[message]</p>
  135. function addError(messages, error, input) {
  136. var block = document.createElement("p");
  137. block.classList.add("text-danger");
  138. block.classList.add("error");
  139. block.innerText = error;
  140. messages.appendChild(block);
  141. $(input).addClass("input-danger");
  142. }
  143. function showSuccess() {
  144. // We made it \:D/
  145. alert("Success!");
  146. }
  147. /*Second form*/
  148. // These are the constraints used to validate the form
  149. var constraints1 = {
  150. Email: {
  151. // Email is required
  152. presence: true,
  153. // and must be an email (duh)
  154. email: true
  155. },
  156. Username: {
  157. // You need to pick a username too
  158. presence: true,
  159. // And it must be between 3 and 20 characters long
  160. length: {
  161. minimum: 3,
  162. maximum: 20
  163. },
  164. format: {
  165. // We don't allow anything that a-z and 0-9
  166. pattern: "[a-z0-9]+",
  167. // but we don't care if the username is uppercase or lowercase
  168. flags: "i",
  169. message: "can only contain a-z and 0-9"
  170. }
  171. }
  172. };
  173. // Hook up the form so we can prevent it from being posted
  174. var form1 = document.querySelector("form#second");
  175. form1.addEventListener("submit", function(ev) {
  176. ev.preventDefault();
  177. handleFormSubmit1(form1);
  178. });
  179. // Hook up the inputs to validate on the fly
  180. var inputs1 = document.querySelectorAll("input, textarea, select")
  181. for (var i = 0; i < inputs.length; ++i) {
  182. inputs.item(i).addEventListener("change", function(ev) {
  183. var errors = validate(form1, constraints1) || {};
  184. showErrorsForInput1(this, errors[this.name])
  185. });
  186. }
  187. function handleFormSubmit1(form, input) {
  188. //
  189. // validate the form aainst the constraints
  190. var errors = validate(form, constraints1);
  191. // then we update the form to reflect the results
  192. showErrors1(form, errors || {});
  193. if (!errors) {
  194. showSuccess1();
  195. }
  196. }
  197. // Updates the inputs with the validation errors
  198. function showErrors1(form, errors) {
  199. //
  200. // We loop through all the inputs and show the errors for that input
  201. _.each(form.querySelectorAll("input[name], select[name]"), function(input) {
  202. // Since the errors can be null if no errors were found we need to handle
  203. // that
  204. showErrorsForInput1(input, errors && errors[input.name]);
  205. });
  206. }
  207. // Shows the errors for a specific input
  208. function showErrorsForInput1(input, errors) {
  209. // This is the root of the input
  210. var formGroup = closestParent1(input.parentNode, "md-input-wrapper")
  211. // Find where the error messages will be insert into
  212. ,
  213. messages = formGroup.querySelector(".messages");
  214. // First we remove any old messages and resets the classes
  215. resetFormGroup1(formGroup);
  216. // If we have errors
  217. if (errors) {
  218. // we first mark the group has having errors
  219. formGroup.classList.add("has-error");
  220. // then we append all the errors
  221. _.each(errors, function(error) {
  222. addError1(messages, error);
  223. });
  224. } else {
  225. // otherwise we simply mark it as success
  226. formGroup.classList.add("has-success");
  227. }
  228. }
  229. // Recusively finds the closest parent that has the specified class
  230. function closestParent1(child, className) {
  231. //
  232. if (!child || child == document) {
  233. return null;
  234. }
  235. if (child.classList.contains(className)) {
  236. return child;
  237. } else {
  238. return closestParent1(child.parentNode, className);
  239. }
  240. }
  241. function resetFormGroup1(formGroup) {
  242. // Remove the success and error classes
  243. formGroup.classList.remove("has-error");
  244. formGroup.classList.remove("has-success");
  245. // and remove any old messages
  246. _.each(formGroup.querySelectorAll(".text-danger"), function(el) {
  247. el.parentNode.removeChild(el);
  248. });
  249. }
  250. // Adds the specified error with the following markup
  251. // <p class="help-block error">[message]</p>
  252. function addError1(messages, error) {
  253. //
  254. var block = document.createElement("i");
  255. block.classList.add("text-danger");
  256. block.classList.add("error");
  257. block.classList.add("icofont");
  258. block.classList.add("icofont-close-circled");
  259. messages.appendChild(block);
  260. $(block).attr("data-toggle", "tooltip");
  261. $(block).attr("data-placement", "top");
  262. $(block).attr("data-trigger", "hover");
  263. $(block).attr("title", error);
  264. $('[data-toggle="tooltip"]').tooltip();
  265. return false;
  266. }
  267. function showSuccess1() {
  268. // We made it \:D/
  269. alert("Success!");
  270. }
  271. /*number validation*/
  272. // These are the constraints used to validate the form
  273. var constraints2 = {
  274. integer: {
  275. // You need to pick a integer too
  276. presence: true,
  277. numericality: {
  278. onlyInteger: true
  279. }
  280. },
  281. numeric: {
  282. // You need to pick a numeric too
  283. presence: true,
  284. numericality: {
  285. onlyNumeric: true
  286. }
  287. },
  288. Number: {
  289. presence: true,
  290. numericality: {
  291. onlyNumeric: true,
  292. greaterThan: 50
  293. }
  294. },
  295. Numbers: {
  296. presence: true,
  297. numericality: {
  298. onlyNumeric: true,
  299. lessThan: 50
  300. }
  301. }
  302. };
  303. // Hook up the form so we can prevent it from being posted
  304. var form2 = document.querySelector("form#number_form");
  305. form2.addEventListener("submit", function(ev) {
  306. ev.preventDefault();
  307. handleFormSubmit2(form2);
  308. });
  309. // Hook up the inputs to validate on the fly
  310. var inputs2 = document.querySelectorAll("input, textarea, select")
  311. for (var i = 0; i < inputs.length; ++i) {
  312. inputs.item(i).addEventListener("change", function(ev) {
  313. var errors = validate(form, constraints2) || {};
  314. showErrorsForInput2(this, errors[this.name])
  315. });
  316. }
  317. function handleFormSubmit2(form, input) {
  318. // validate the form aainst the constraints
  319. var errors = validate(form, constraints2);
  320. // then we update the form to reflect the results
  321. showErrors2(form, errors || {});
  322. if (!errors) {
  323. showSuccess2();
  324. }
  325. }
  326. // Updates the inputs with the validation errors
  327. function showErrors2(form, errors) {
  328. // We loop through all the inputs and show the errors for that input
  329. _.each(form.querySelectorAll("input[name], select[name]"), function(input) {
  330. // Since the errors can be null if no errors were found we need to handle
  331. // that
  332. showErrorsForInput2(input, errors && errors[input.name]);
  333. });
  334. }
  335. // Shows the errors for a specific input
  336. function showErrorsForInput2(input, errors) {
  337. // This is the root of the input
  338. var formGroup = closestParent2(input.parentNode, "md-input-wrapper")
  339. // Find where the error messages will be insert into
  340. ,
  341. messages = formGroup.querySelector(".messages");
  342. // First we remove any old messages and resets the classes
  343. resetFormGroup2(formGroup);
  344. // If we have errors
  345. if (errors) {
  346. // we first mark the group has having errors
  347. formGroup.classList.add("has-error");
  348. // then we append all the errors
  349. _.each(errors, function(error) {
  350. addError2(messages, error, input);
  351. });
  352. } else {
  353. // otherwise we simply mark it as success
  354. formGroup.classList.add("has-success");
  355. }
  356. }
  357. // Recusively finds the closest parent that has the specified class
  358. function closestParent2(child, className) {
  359. if (!child || child == document) {
  360. return null;
  361. }
  362. if (child.classList.contains(className)) {
  363. return child;
  364. } else {
  365. return closestParent2(child.parentNode, className);
  366. }
  367. }
  368. function resetFormGroup2(formGroup) {
  369. // Remove the success and error classes
  370. formGroup.classList.remove("has-error");
  371. formGroup.classList.remove("has-success");
  372. // and remove any old messages
  373. _.each(formGroup.querySelectorAll(".text-danger"), function(el) {
  374. el.parentNode.removeChild(el);
  375. });
  376. }
  377. // Adds the specified error with the following markup
  378. // <p class="help-block error">[message]</p>
  379. function addError2(messages, error, input) {
  380. var block = document.createElement("p");
  381. block.classList.add("text-danger");
  382. block.classList.add("error");
  383. block.innerText = error;
  384. messages.appendChild(block);
  385. $(input).addClass("input-danger");
  386. }
  387. function showSuccess2() {
  388. // We made it \:D/
  389. alert("Success!");
  390. }
  391. /*checkbox and dropdown validation*/
  392. var constraints3 = {
  393. member: {
  394. // You need to pick a member too
  395. presence: true,
  396. },
  397. Language: {
  398. presence: true,
  399. }
  400. };
  401. // Hook up the form so we can prevent it from being posted
  402. var form3 = document.querySelector("form#checkdrop");
  403. form3.addEventListener("submit", function(ev) {
  404. ev.preventDefault();
  405. handleFormSubmit3(form3);
  406. });
  407. // Hook up the inputs to validate on the fly
  408. var inputs3 = document.querySelectorAll("input, textarea, select")
  409. for (var i = 0; i < inputs3.length; ++i) {
  410. inputs.item(i).addEventListener("change", function(ev) {
  411. var errors = validate(form3, constraints3) || {};
  412. showErrorsForInput3(this, errors[this.name])
  413. });
  414. }
  415. function handleFormSubmit3(form, input) {
  416. // validate the form aainst the constraints
  417. var errors = validate(form, constraints3);
  418. // then we update the form to reflect the results
  419. showErrors3(form, errors || {});
  420. if (!errors) {
  421. showSuccess3();
  422. }
  423. }
  424. // Updates the inputs with the validation errors
  425. function showErrors3(form, errors) {
  426. // We loop through all the inputs and show the errors for that input
  427. _.each(form.querySelectorAll("input[name], select[name]"), function(input) {
  428. // Since the errors can be null if no errors were found we need to handle
  429. // that
  430. showErrorsForInput3(input, errors && errors[input.name]);
  431. });
  432. }
  433. // Shows the errors for a specific input
  434. function showErrorsForInput3(input, errors) {
  435. // This is the root of the input
  436. var formGroup = closestParent3(input.parentNode, "md-input-wrapper")
  437. // Find where the error messages will be insert into
  438. ,
  439. messages = formGroup.querySelector(".messages");
  440. // First we remove any old messages and resets the classes
  441. resetFormGroup3(formGroup);
  442. // If we have errors
  443. if (errors) {
  444. // we first mark the group has having errors
  445. formGroup.classList.add("has-error");
  446. // then we append all the errors
  447. _.each(errors, function(error) {
  448. //
  449. addError3(messages, error, input);
  450. });
  451. } else {
  452. // otherwise we simply mark it as success
  453. formGroup.classList.add("has-success");
  454. }
  455. }
  456. // Recusively finds the closest parent that has the specified class
  457. function closestParent3(child, className) {
  458. if (!child || child == document) {
  459. return null;
  460. }
  461. if (child.classList.contains(className)) {
  462. return child;
  463. } else {
  464. return closestParent3(child.parentNode, className);
  465. }
  466. }
  467. function resetFormGroup3(formGroup) {
  468. // Remove the success and error classes
  469. formGroup.classList.remove("has-error");
  470. formGroup.classList.remove("has-success");
  471. // and remove any old messages
  472. _.each(formGroup.querySelectorAll(".text-danger"), function(el) {
  473. el.parentNode.removeChild(el);
  474. });
  475. }
  476. // Adds the specified error with the following markup
  477. // <p class="help-block error">[message]</p>
  478. function addError3(messages, error, input) {
  479. var block = document.createElement("p");
  480. block.classList.add("text-danger");
  481. block.classList.add("error");
  482. block.innerText = error;
  483. messages.appendChild(block);
  484. $(input).addClass("input-danger");
  485. }
  486. function showSuccess3() {
  487. // We made it \:D/
  488. alert("Success!");
  489. }
  490. });