Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

Students also viewed these Databases questions

Question

1. Explain how business strategy affects HR strategy.

Answered: 1 week ago

Question

What are the stages of project management? Write it in items.

Answered: 1 week ago

Question

why do consumers often fail to seek out higher yields on deposits ?

Answered: 1 week ago