/    Sign up×
Community /Pin to ProfileBookmark

Adding label with message, instead of alert

I am trying to validate a form, and below is a basic code just for understanding: (currently with name and password only). What I want is, Instead of alerting when something is wrong, is there a way to add a message below each field such as “The name is not valid” or “Password must be at least 6 characters long”?

The form:

[code=html]<form name=”myform” method=”post” action=”abc.jsp” onsubmit=”return validateform()” >
Name: <input type=”text” name=”name”><br/>
Password: <input type=”password” name=”password”><br/>
<input type=”submit” value=”register”>
</form>
[/code]

[CODE]function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;

if (name==null || name==””){
alert(“Name can’t be blank”);
return false;
}else if(password.length<6){
alert(“Password must be at least 6 characters long.”);
return false;
}
} [/CODE]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 25.2016 — I recommend having a look at HTML5 form validation. This does checking and displaying messages automatically.
Copy linkTweet thisAlerts:
@lioshiauthorOct 25.2016 — But HTML can't tell if a name is valid, for example DSF#23t23 is also a name, I want some check before submitting. Is it done by JQuery ? (To add labels with error messages?) Or something else?

What about plugins such as things I've just discovered while looking for help: ParsleyJS, JQuery Form Validation plugin? Are they safe? They just do a real nice job, but not sure what's behind it, as I can't see the code
Copy linkTweet thisAlerts:
@SempervivumOct 25.2016 — You can do the checking by a pattern. This pattern can be a regular expressing which is a very powerful and flexible Tool for analysing strings. Things like JQuery Form Validation plugin are obsolete since there is HTML5 form validation.
Copy linkTweet thisAlerts:
@lioshiauthorOct 25.2016 — Thank you! DIdn't know about that, looks great. But, I saw it's not compatible with Safari?
Copy linkTweet thisAlerts:
@SempervivumOct 25.2016 — Where did you find the information that Safari doesn't support it? That would be too bad.

I wasn't able to find informatio about browser support.
Copy linkTweet thisAlerts:
@Kevin2Oct 25.2016 — Where did you find the information that Safari doesn't support it?[/QUOTE]

http://www.w3schools.com/tags/att_input_pattern.asp

More complete info:

http://caniuse.com/#search=pattern
Copy linkTweet thisAlerts:
@SempervivumOct 25.2016 — Thanks, I googled for "html5 form validation browser support".
Copy linkTweet thisAlerts:
@rootOct 26.2016 — According to https://html5test.com/compare/browser/safari-9.0.html the form validation is supported.
×

Success!

Help @lioshi 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 5.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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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