Advertisement
TZinovieva

Dynamic Validation JS Advanced

Sep 27th, 2023
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function validate() {
  2.     const inputElement = document.getElementById('email');
  3.     const validMatch = /^[a-z]+@[a-z]+\.[a-z]+$/;
  4.  
  5.     inputElement.addEventListener('change', (e) => {
  6.         if (!validMatch.test(inputElement.value)) {
  7.             e.currentTarget.classList.add('error');
  8.         } else {
  9.             e.currentTarget.classList.remove('error');
  10.         }
  11.     });
  12. }
  13.  
  14. validate(); // Call the function to set up the event listener
  15.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement