Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. /// <reference path="../typings/index.d.ts"/>
  2.  
  3. function validate() {
  4. let userName = $('#username');
  5. let email = $('#email');
  6. let pass = $('#password');
  7. let comPass = $('#confirm-password');
  8. let company = $('#company');
  9. let companyNumber = $('#companyNumber');
  10. let submitBtn = $('#submit');
  11. submitBtn.attr('href', '#');
  12.  
  13. let checked = true;
  14. let isPassOK = false;
  15. company.on('click', displayCompanyInfo);
  16. submitBtn.on('click', checkForm);
  17. userName.on('change', checkUserName);
  18. email.on('change', checkEmail);
  19. pass.on('change', checkPass);
  20. comPass.on('change', checkComPass);
  21. companyNumber.on('change', checkCompany);
  22.  
  23. function checkCompany() {
  24.  
  25. if (companyNumber.val().length === 4) {
  26. let num = Number(companyNumber.val());
  27. if (num >= 1000 && num <= 9999) {
  28. companyNumber.css('border-color', '');
  29. } else {
  30. companyNumber.css('border-color', 'red');
  31. }
  32. } else {
  33. companyNumber.css('border-color', 'red');
  34. }
  35. }
  36.  
  37. function checkComPass() {
  38. if (isPassOK && (pass.val() == comPass.val())) {
  39. comPass.css('border-color', '');
  40. } else {
  41. comPass.css('border-color', 'red');
  42. }
  43. }
  44.  
  45. function checkPass() {
  46. const regex = /^[a-z0-9_]{5,15}$/gi;
  47. if (pass.val().match(regex)) {
  48. isPassOK = true;
  49. pass.css('border-color', '');
  50. } else {
  51. isPassOK = false;
  52. pass.css('border-color', 'red');
  53. }
  54. }
  55.  
  56. function checkEmail() {
  57. const regex = /^.*@.*[\.]+.*$/gi;
  58. email.val().match(regex) ?
  59. email.css('border-color', '') :
  60. email.css('border-color', 'red');
  61. }
  62.  
  63. function checkUserName() {
  64. const regex = /^[a-z0-9]{3,20}$/gi;
  65. userName.val().match(regex) ?
  66. userName.css('border-color', '') :
  67. userName.css('border-color', 'red');
  68. }
  69.  
  70. function checkForm(event) {
  71. event.preventDefault();
  72. if (document.getElementById('companyInfo').style.display == 'none') {
  73. normalCheck();
  74. let doc = Array.from(document.querySelectorAll('#userInfo input'));
  75. doc.every(x => x.style.borderColor == '') ?
  76. $('#valid').css('display', 'inline') :
  77. $('#valid').css('display', 'none');
  78. } else {
  79. normalCheck();
  80. checkCompany();
  81. let doc = Array.from(document.querySelectorAll('#registerForm input'));
  82. doc.every(x => x.style.borderColor == '') ?
  83. $('#valid').css('display', 'inline') :
  84. $('#valid').css('display', 'none');
  85. }
  86. }
  87.  
  88. function normalCheck() {
  89. checkUserName();
  90. checkEmail();
  91. checkPass();
  92. checkComPass();
  93. }
  94.  
  95. function displayCompanyInfo() {
  96. if (checked === true) {
  97. $('#companyInfo').css('display', 'inline');
  98. checked = false;
  99. } else {
  100. $('#companyInfo').css('display', 'none');
  101. checked = true;
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement