Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript jQuery validation , +
Objective: I'd like to display the focused field error message in a container.
 
What I've done so far:
<!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 name@domain.com."
     },
     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>
Demo: http://dl.dropbox.com/u/4017788/Labs/sample-form.html[^]
 
Problems:
1. If you click the submit button, the container(span) shows the first error message, no matter which field was focused.
2. Focusing on fields using the Tab key works well (except on the URL field), but focusing with a mouse doesn't update the span HTML correctly.
Posted 19-Nov-12 3:45am
Edited 20-Nov-12 1:15am
v3
Comments
Killzone DeathMan at 23-Nov-12 5:03am
   
Did you know that jquery validation is bad? Jquery is at the client side, the client just delete the local code to validate and the he can register without a Name or without a valid email, etc.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just go through the article Basic jQuery Form Validation Example (2mins)[^] and try to implement the codes.
It is working fine as per your requirements.
You can take a look at the live demo[^] also.
 
Thanks...
Comments
RainLover at 19-Nov-12 11:28am
   
Thanks for the answer, but it's not what I'm looking for -- I'd like to display only the focused field error message. Please see the demo.
Tadit Dash at 20-Nov-12 1:31am
   
Ideally, showing all the validations is a good practice.
It shows all the validations, but focuses only that field, which is currently focused (which you wanted to do).
And if you can go through the codes and tutorial, then you can customize according to your requirements, if you really need those.
But, in my opinion, the demo is working correctly if you take user's experience into account.
 
Please reply what you feel about it.
 
Thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 175
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100