Advertisement
AlexPshkov

js

May 10th, 2022 (edited)
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Handle submit button
  3.  */
  4. signUpForm.onsubmit = async (e) => {
  5.     e.preventDefault();
  6.  
  7.     let isAllowed = true;
  8.  
  9.     for (let child of signUpForm.children) { //Check all fields
  10.         let value = child.value.toString();
  11.         let isCorrectFlag = (value.length > 0);
  12.         if (child.name === "username" && isCorrectFlag) {
  13.             isCorrectFlag = !(/[^a-zA-zа-яА-Я]/.test(value));
  14.         }
  15.         if (child.name === "email" && isCorrectFlag) {
  16.             isCorrectFlag = /\S+@\S+\.\S+/.test(value);
  17.         }
  18.         if (isCorrectFlag) child.classList.remove("invalid-field");
  19.         else {
  20.             child.classList.add("invalid-field");
  21.             isAllowed = false;
  22.         }
  23.            
  24.     }
  25.  
  26.     if (!userPolicy.checked) return; //Check if policy not accepted
  27.  
  28.     if (!isAllowed) return;
  29.     let ajax = fetch('./fileHandler.php', { //Generate fetch request
  30.         method: 'POST',
  31.         body: new FormData(signUpForm)
  32.     });
  33.  
  34.     ajax.then(result => result.json().then(jsonResult => { //Make async request
  35.         if (jsonResult.status !== 200) modalContent.innerHTML = jsonResult.message;
  36.         else closeModalWindow();
  37.     }).catch(() => modalContent.innerHTML = errorText)).catch(() => modalContent.innerHTML = errorText); //Catch exceptions
  38. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement