//Gets the DOM of the from const form = document.getElementById('newVendorForm'); //Gets the DOM of the description text area seperately because it is not part of the form const serviceBox = document.getElementById('desc'); //Initializes an event listener on the submit button of the form form.addEventListener('submit', (e) => { e.preventDefault(); //Prevents the page from refreshing //Retrieves the values of all input fields in the form and saves it into a data object const formData = new FormData(form); const data = Object.fromEntries(formData); //Retieves the values of the description box const service = serviceBox.value; //Checks if the information is missing or invalid if (data.companyName == "") { alert("Please fill out your companies name") } else if (data.location == ""){ alert("Please fill out your location") } else if (service == "") { alert("Please fill out your companies services provided") } else if (ValidateInformation(data.phoneNumber, data.email)){ //Validate the email and phone number //This is where the code to send the email will go in the next unit, utilizing a library alert("Success"); } }); //Checks if the string is only numbers function isOnlyNumbers(phoneNo) { //Loops through each character in the string and checks if it is a number for (var i = 0; i < phoneNo.length; i++) { //Gets the character code of the current character charCode = phoneNo[i].charCodeAt(); //Char code 48 is 0 and 57 is 9 with inbetween being the numbers 1-9. If the char code is not in this range then it is not a number and the function should return false if (charCode < 48 || charCode > 57) { return false; } } //If the loop completes then the string is only numbers return true; } //Valdates phone number by checking if it is only numbers and if it is 10 digits function ValidateNo(phoneNo) { //Checks if the phone number is empty if (phoneNo == "" || phoneNo == null) { alert("Please enter your Mobile No."); return false; } //Checks if the phone number is the correct length else if (phoneNo.length < 10 || phoneNo.length > 10) { alert("Mobile No. is not valid, Please Enter 10 Digit Mobile No."); return false; } //Calls the function to check if the phone number is only numbers else if (!isOnlyNumbers(phoneNo)){ alert("Please enter only Numbers for your Phone Number"); } //If all the checks pass then the phone number is valid return true; } //Validated email by checking if it is in the correct format function ValidateEmail(email) { //Defines the regular expression for an email var mailFormat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; //Checks if the email is empty if (email == "") { alert( " Please enter your Email Id "); } //Checks if the email is in the correct format else if (!mailFormat.test(email)) { alert( " Email Address is not valid, Please provide a valid Email "); return false; } //Returns true if the the other checks pass else { return true; } } //Ensure both email and phone number are valid function ValidateInformation(phoneNo, email){ if (ValidateNo(phoneNo) && ValidateEmail(email)){ return true; } else { return false; } }