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
Test Header
- Home
- About Us
- Products
h2 {
text-align: center;
font-size: 40px;
}
Contact Us
label {
display: inline-block;
width: 660px;
text-align: right;
}
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started