/    Sign up×
Community /Pin to ProfileBookmark

Form Validation (novice at Javascript)

I’m building a multi page quote application and when an <input> is filled out incorrectly the .js will inject a message via a <span> under the <input>.
That code appears to be working fine.

My issue is when multiple <inputs> are incorrectly filled out then corrected except one, the corrected <inputs> dont remove the injected message.

Could i have help with my code.
or maybe there is a better way to achieve this.

[upl-image-preview url=https://www.webdeveloper.com/assets/files/2020-01-24/1579904367-72907-form.png]

Javascript

[code]var firstname, surname, email;

function _(x) {
return document.getElementById(x);
}
function processPhase1 () {
firstname = _(‘firstname’).value;
surname = _(‘surname’).value;
email = _(’email’).value;

if ((firstname == ”) || (!isNaN(firstname)) || (firstname.length <3)) {
_(‘firstnameError’).innerHTML = “please provide a first name for the inquiry”;
return false;
}
if ((surname == ”) || (!isNaN(surname)) || (surname.length <3)) {
_(‘surnameError’).innerHTML = “please provide a surname for the inquiry”;
return false;
}
if ((email.indexOf(‘@’) <= 0) || (email == ”)) {
_(’emailError’).innerHTML = “please provide an email for the inquiry”;
return false;
} [/code]

HTML

[code]<input id=”firstname” type=”text” placeholder=”First Name*” class=”name” name=”firstname”>
<span id=”firstnameError”></span>[/code]

Edited by site staff: Inserted code tags [code]your code here[/code]

to post a comment
HTMLJavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 25.2020 — maybe there is a better way to achieve this.[/quote]Yes, there is: Make yourself familiar with HTML5 form validation, it will make your task much easier:

https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation

Regarding your main question: I don't understand it completely:

In your code I don't find the section where the error message is inserted into the span. And I don't find how the function you posted is called.
×

Success!

Help @josh-josh-josh 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.18,
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,
)...