Question: I am having trouble with my Javascript not doing the correct validation on the Phone Number and Email fields. I need the validation done in

I am having trouble with my Javascript not doing the correct validation on the Phone Number and Email fields. I need the validation done in Javascript to ensure that the phone number is 10 digits else an error is displayed and the email address is in the correct format. For some reason, the correct error messages aren't displaying as per the HTML and JS.

My HTML

contactUs

Test Header

Contact Us

Enter your name

Enter an email

Enter a phone number

Tell us how we can help you

My JS

document.addEventListener("DOMContentLoaded", domLoaded);

function domLoaded() {

document.getElementById("submitBtn").addEventListener("click", function() {

let uname = document.getElementById("name").value;

let email = document.getElementById("email").value;

let phone = document.getElementById("phone").value;

let help = document.getElementById("help").value;

let reg = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;

if(uname = "" || email == "" || phone == "" || help == ""){

alert("All fields must be filled out");

return false;

}

else if(!reg.contactUs(email)){

alert("Invalid email.Please enter valid email");

return false;

}

else if(phone.length != 10 || !Number(phone)){

alert("Please enter 10 digit phone number");

return false;

}

else{

alert("All data has been sent succefully")

}

})

};

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!