Advertisement
orenchuck

soda email validation with custom errors

May 27th, 2022 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. <style>
  2. .adoric-custom-error {
  3. width: 200px;
  4. height: 40px;
  5. font-size: 11px;
  6. color: red;
  7. opacity: 1;
  8. position: absolute;
  9. top: 25px;
  10. left: 15px;
  11. display: none;
  12. z-index: 100;
  13. }
  14.  
  15. @media screen and (max-width: 800px) {
  16. .adoric-custom-error {
  17. top: 30px;
  18. font-size: 10px;
  19. }
  20. }
  21.  
  22. @media screen and (max-width: 450px) {
  23. .adoric-custom-error {
  24. top: 25px;
  25. font-size: 9px;
  26. }
  27. }
  28. </style>
  29. <script>
  30. (function() {
  31. const errorMessage1 = 'Please, do not use +';
  32. const errorMessage2 = 'Please, do not use @ after .';
  33. const emailField = document.querySelector('.' + self.identifier + ' [tabindex] INPUT[name="email"]');
  34. const submitButton = document.querySelector('.' + self.identifier + ' [tabindex] INPUT[name="submit"]');
  35. const current = document.querySelector('.' + self.identifier + ' [tabindex]');
  36. const errorBlock = document.createElement('div');
  37. errorBlock.className = 'adoric-custom-error';
  38. emailField.after(errorBlock);
  39.  
  40. if (emailField) {
  41. emailField.addEventListener('input', function() {
  42. setTimeout(function() {
  43. const emailFieldValue = emailField.value;
  44. const regexpPlus = /\+/;
  45. const regexpTest = /\+soda_test/i;
  46. const regexpMail = /\.@/;
  47. submitButton.style.pointerEvents = 'auto';
  48. errorBlock.style.display = 'none';
  49.  
  50. if (regexpPlus.test(emailFieldValue)) {
  51. submitButton.style.pointerEvents = 'none';
  52. showValidationmessage(errorMessage1);
  53. if (regexpTest.test(emailFieldValue)) {
  54. submitButton.style.pointerEvents = 'auto';
  55. errorBlock.style.display = 'none';
  56. }
  57. }
  58. if (regexpMail.test(emailFieldValue)) {
  59. submitButton.style.pointerEvents = 'none';
  60. showValidationmessage(errorMessage2);
  61. }
  62. }, 150);
  63. });
  64. }
  65.  
  66. function showValidationmessage(message) {
  67. errorBlock.style.display = 'block';
  68. errorBlock.textContent = message;
  69. }
  70. })();
  71. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement