forms.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const passwordRegex = /[A-Z0-6!"\#$%&'()*+,\-./:;<=>?@\[\\\]^_‘{|}~]/;
  2. const fullNameRegex = /^[\w]+[\w ]*$/;
  3. function initControls() {
  4. $('.inpt, .password').find('.icon').mousedown(function(e) {
  5. if ($(e.target).parent().hasClass('password')) {
  6. $(e.target).parent().find('input').attr('type', 'text');
  7. }
  8. });
  9. $('.inpt, .password').find('.icon').mouseup(function(e) {
  10. if ($(e.target).parent().hasClass('password')) {
  11. $(e.target).parent().find('input').attr('type', 'password');
  12. }
  13. });
  14. }
  15. function addValidation(field, form, func) {
  16. func(field);
  17. $(field).on('input', function(e) {func(e.target, form)});
  18. }
  19. function validateForm(form) {
  20. if (form == null) {
  21. return false;
  22. }
  23. if ($(form).find('.inpt').hasClass('bad')) {
  24. $(form).find('.btn').addClass('disabled');
  25. return false;
  26. }
  27. $(form).find('.btn').removeClass('disabled');
  28. return true;
  29. }
  30. function validatePassword(name, form) {
  31. var element = $(name);
  32. var fieldDiv = element.parent();
  33. if (element.val() != '') {
  34. fieldDiv.removeClass('bad');
  35. if (element.val().length < 8 || !passwordRegex.test(element.val())) {
  36. fieldDiv.addClass('weak');
  37. } else {
  38. fieldDiv.removeClass('weak');
  39. }
  40. } else {
  41. fieldDiv.removeClass('weak');
  42. fieldDiv.addClass('bad');
  43. }
  44. validateForm(form);
  45. }
  46. function validateFullName(name, form) {
  47. var element = $(name);
  48. var fieldDiv = element.parent();
  49. if (fullNameRegex.test(element.val())) {
  50. fieldDiv.removeClass('bad');
  51. } else {
  52. fieldDiv.addClass('bad');
  53. }
  54. validateForm(form);
  55. }
  56. function validateField(element, form) {
  57. var fieldDiv = element.parent();
  58. if (element.val() != '') {
  59. fieldDiv.removeClass('bad');
  60. } else {
  61. fieldDiv.addClass('bad');
  62. }
  63. validateForm(form);
  64. }
  65. //Email validation
  66. const emailRegex = /^[a-zA-Z]+[\w\d\._-]*$/;
  67. var emailInputTimer = null;
  68. function validateEmail(name, form) {
  69. var element = $(name);
  70. var fieldDiv = element.parent();
  71. fieldDiv.addClass('bad');
  72. if (emailRegex.test(element.val())) {
  73. clearTimeout(emailInputTimer);
  74. emailInputTimer = setTimeout(function(){
  75. $.ajax({
  76. url: '/checkEmail',
  77. data: {
  78. user: element.val()
  79. },
  80. success: function(result) {
  81. fieldDiv.removeClass('bad');
  82. validateForm(form);
  83. },
  84. error: function(jqXHR, textStatus, errorThrown) {
  85. fieldDiv.addClass('bad');
  86. validateForm(form);
  87. }
  88. });
  89. }, 200);
  90. return;
  91. } else {
  92. validateForm(form);
  93. }
  94. }