/    Sign up×
Community /Pin to ProfileBookmark

How do I fix this “Submit is undefined” error?

I’m working on coding a contact form as part of my homework assignment. If the e-mail, re-type e-mail, or phone text inputs are left blank, it is supposed to display to the user an error message that says, “Please enter your email and phone number to be contacted with.” The javascript code is supposed to target the parts of the form with the input label=”contactInfo” to check to see if any of those are left blank and display the error message if they are blank. In the browser console, when I click submit, it says, “submit is not defined.”

The line of javascript code with the error is identified in the console as submit.Btn.addEventListener(“click”, ValidateForm, false);

Any help anyone can give is appreciated. (I am new to javascript.) Thank you for any help you give.

HTML

<form action=”/action_page.php”>

First name:
<input type=”text” size=”35″ name=”firstName” value=”Your First Name”><br>
Last name:
<input type=”text” size=”35″ name=”lastName” value=”Your Last Name”><br>
E-Mail:
<input label=”contactInfo” type=”text” size=”35″ name=”email” value=”E-Mail”><br>
Re-Type E-Mail:
<input label=”contactInfo” type=”text” size=”35″ name=”retypeEmail” value=”Re-Type E-Mail”><br>
Phone:
<input label=”contactInfo” type=”text” size=”35″ name=”phone” value=”Phone #”><br>
Best time of day to call (Optional):
<input type=”text” size=”35″ name=”timeToCall” value=”Best Time to Call”><br>
Do you prefer Email or Phone Number?:
<input type=”radio” name=”contact” value=”Email”> Email
<input type=”radio” name=”contact” value=”Phone Number”> Phone Number<br>
Comments:<br>
<textarea rows=”8″ cols=”50″ name=”comment” form=”usrForm”>
Enter text here…</textarea><br>
<input id=”btnSubmit” type=”submit” value=”submit”>
</form>

JAVASCRIPT

<script>
function validateForm(event) {
event.preventDefault ();
var formHasErrors = false;
try {
var contactInfo = document.getElementById(“lName”).value;
if (contactInfo === “”) {
throw “Please enter your email and phone number to be contacted with.”;
}
}
catch (errMsg) {
console.log(errMsg);
formHasErrors = true;
document.getElementById(“lNameErr”).innerHTML = errMsg;
document.getElementById(“lNameErr”).style.display = “block”;
}
finally {
if (formHasErrors) {
console.log(“Please fix all the form errors”);
}
}

}

function createEventListeners() {
var submitBtn = document.getElementById(“btnSubmit”);
submit.Btn.addEventListener(“click”, ValidateForm, false);
}

window.addEventListener(“load”, createEventListeners, false);
</script>

to post a comment
HTMLJavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 18.2019 — > @TacoSpaceRaptor#1608857 submit.Btn.addEventListener("click", ValidateForm, false);

I think you just need to get rid of the dot between "submit" and "Btn"? (The variable name you create before that is submitBtn.)
×

Success!

Help @TacoSpaceRaptor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...