Advertisement
ralitsa_d

Untitled

Oct 22nd, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. function solve() {
  2.  
  3. let username = "",
  4. email= "",
  5. password = "",
  6. confirmPassword = "",
  7. companyNumber = "";
  8.  
  9. return function(){
  10.  
  11. let formIsValid = false;
  12. let usernameValid = false;
  13. let emailValid = false;
  14. let passwordValid = false;
  15. let passwordConfirmValid = false;
  16. let companyNumberValid = false;
  17.  
  18. $("#registerForm").submit(function(e) {
  19. e.preventDefault();
  20. });
  21.  
  22. $("#company").on("change", function () {
  23. if ($("#company").is(":checked")){
  24. $("#companyInfo").show()
  25. }
  26. else{
  27. $("#companyInfo").hide();
  28. }
  29. });
  30.  
  31. function isMatch(str, pattern) {
  32. let regex = new RegExp(pattern);
  33. let match = regex.test(str);
  34. return match;
  35. }
  36.  
  37. let submitBtn = $("#submit");
  38. submitBtn.on("click", function () {
  39.  
  40. username = {
  41. value: $("#username").val(),
  42. pattern: "^[a-zA-Z0-9]{3,20}$",
  43. isValid: (str, pattern) => isMatch(str, pattern)
  44. };
  45.  
  46. email = {
  47. value: $("#email").val(),
  48. pattern: "^.*@.*\..*$",
  49. isValid: (str, pattern) => isMatch(str, pattern)
  50. };
  51.  
  52. password = {
  53. value: $("#password").val(),
  54. pattern: "^[\\w]{5,15}$",
  55. isValid: (str, pattern) => isMatch(str, pattern)
  56. };
  57.  
  58. confirmPassword = {
  59. value: $("#confirm-password").val(),
  60. pattern: "^[\\w]{5,15}$"
  61. };
  62.  
  63. if ($("#company").is(":checked")){
  64. companyNumber = {
  65. value: $("#companyNumber").val(),
  66. pattern: "^[1-9][0-9]{3}$",
  67. isValid: (str, pattern) => isMatch(str, pattern)
  68. };
  69. }
  70.  
  71. if (!username.value || !username.isValid(username.value, username.pattern)){
  72. usernameValid = false;
  73. $('#username').css('border-color', 'red');
  74. }
  75. else{
  76. usernameValid = true;
  77. $('#username').css('border-color', '');
  78. }
  79.  
  80. if (!email.value || !email.isValid(email.value, email.pattern)) {
  81. emailValid = false;
  82. $('#email').css('border-color', 'red');
  83. }
  84. else{
  85. emailValid = true;
  86. $('#email').css('border-color', '');
  87. }
  88.  
  89.  
  90. if (!password.value || !password.isValid(password.value, password.pattern)){
  91. passwordValid = false;
  92. $('#password').css('border-color', 'red');
  93.  
  94. }
  95. else{
  96. passwordValid = true;
  97. $('#password').css('border-color', '');
  98. }
  99. //console.log(password.isValid(password.value, password.pattern));
  100. //console.log(password.pattern);
  101.  
  102.  
  103. if (!confirmPassword.value || password.value != confirmPassword.value){
  104. passwordConfirmValid = false;
  105. $('#confirm-password').css('border-color', 'red');
  106. }
  107. else{
  108. passwordConfirmValid = true;
  109. $('#confirm-password').css('border-color', '');
  110. }
  111.  
  112.  
  113. if (($('#company').is(":checked") &&
  114. !companyNumber.value) ||
  115. ($('#company').is(":checked") &&
  116. !companyNumber.isValid(companyNumber.value, companyNumber.pattern))){
  117. companyNumberValid = false;
  118. $('#companyNumber').css('border-color', 'red');
  119. }
  120. else if($('#company').is(":checked") &&
  121. companyNumber.isValid(companyNumber.value, companyNumber.pattern)){
  122. companyNumberValid = true;
  123. $('#companyNumber').css('border-color', '');
  124. }
  125. else if(!($('#company').is(":checked"))){
  126. companyNumberValid = true;
  127. }
  128.  
  129. formIsValid = !!(usernameValid &&
  130. emailValid &&
  131. passwordValid &&
  132. passwordConfirmValid &&
  133. companyNumberValid);
  134.  
  135. // console.log(usernameValid);
  136. // console.log(emailValid);
  137. // console.log(passwordValid);
  138. // console.log(passwordConfirmValid);
  139. // console.log(companyNumberValid);
  140.  
  141. if (formIsValid){
  142. $('#valid').css('display', '');
  143. }
  144. else{
  145. $('#valid').css('display', 'none');
  146. }
  147. });
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement