/    Sign up×
Community /Pin to ProfileBookmark

jQuery validation: display the focused field error message

[B]Objective[/B]: I’d like to display the focused field error message in a container.

[B]What I’ve done so far[/B]:

[CODE]<!DOCTYPE html>
<html>
<head>
<title></title>
<style type=”text/css”>
label {display:inline-block; width:60px;}
</style>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js” type=”text/javascript”></script>
<script src=”http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js” type=”text/javascript”></script>
<script type=”text/javascript”>
$(document).ready(function(){
$(“form”).validate({
messages: {
name: “Please specify your name.”,
email: {
required: “We need your email address to contact you.”,
email: “Your email address must be in the format of [email protected].”
},
url: “A valid URL, please.”,
comment: “Please enter your comment.”
},
showErrors: function(errorMap, errorList) {
if(errorList.length) {
$(“span”).html(errorList[0].message);
}
}
});
});
</script>
</head>
<body>
<span></span>
<form action=”#”>
<div><label for=”entry_0″>Name *</label><input type=”text” class=”required” name=”name” id=”entry_0″></div>
<div><label for=”entry_1″>Email *</label><input type=”text” class=”required email” name=”email” id=”entry_1″></div>
<div><label for=”entry_2″>URL</label><input type=”text” class=”url” name=”url” id=”entry_2″></div>
<div><textarea class=”required” name=”comment” rows=”7″ cols=”35″></textarea></div>
<div><input type=”submit” name=”submit” value=”Submit”></div>
</form>
</body>
</html>[/CODE]

[B]Problems[/B]:

[LIST=1]

  • [*]

    If you click the submit button, the container(span) shows the first error message, no matter which field was focused.


  • [*]

    Focusing on fields using the Tab key works well, but focusing with a mouse doesn’t update the span HTML correctly.


  • [/LIST]

    to post a comment
    JavaScript

    0Be the first to comment 😎

    ×

    Success!

    Help @Rain_Lover 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.17,
    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,
    )...