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 20KB

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