/    Sign up×
Community /Pin to ProfileBookmark

customized error DIV with jQuery validation & context help

Hi all,

I am trying to do this quite complex Javascript operation for a form. Basically the idea is to show context help when an input is focused. But when there is error when outfocus, it will show an error instead. The context help DIV and error DIV are hidden by default and stay next to each other at the same level (code below).

I finished the context help part. However, I don’t understand the fundamental of jQuery validation plugin ([url]http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js[/url]). The point is not to use error message (LABEL) of the plugin itself but just SHOW the error DIV that I already had. I read the errorPlacement: function(error, element) feature. However, I didn’t think I have done the right thing as you can see below [I]element.parent().next().next().fadeOut(‘fast’);[/I]

Any help would be appreciate!! Thanks.

This is the code for Javascript:

[CODE]
<script type=”text/javascript”>
$(document).ready(function() {

// CONTEXT HELP
$(‘table .contexthelp’).hide();

$(‘input’).focus(function(){
$(this).parent().next().fadeIn(‘fast’);
$(this).parent().next().next().hide();
//$(this).css(‘color’, ‘#333;’);
});
$(‘input’).blur(function(){
$(this).parent().next().fadeOut(‘fast’);
});

$(“#frmregister”).validate({

rules: {
username: “required”,
email: {
required: true,
email: true
}
},
errorPlacement: function(error, element) {
element.parent().next().next().fadeOut(‘fast’);
},
debug:true

});

});
</script>[/CODE]

This is the code for the form:

[CODE]
<form method=”post” action=”#” id=”frmregister”>
<h2 class=”reg-title”>1. Thông tin &#273;&#259;ng nh&#7853;p</h2>
<div class=”reg-line”>
<div class=”reg-left”>
<label for=”” class=”reg-label”>Username</label>
</div>
<div id=”” class=”field-required”>
<input id=”username” name=”username” class=”input-text” maxlength=”50″ name=”” value=”” />
</div>
<div id=”testid” class=”field-msg” style=”display:none;”><img src=”images/arrow_msg.gif” width=”13″ height=”13″/>Context help for username</div>
<div id=”” class=”field-msg-error” style=”display:none;”><img src=”images/arrow_msg_error.gif” width=”13″ height=”13″ style=””/>Error msg for username</div>
</div>
<div class=”reg-line”>
<div class=”reg-left”>
<label for=”” class=”reg-label”>Email</label>
</div>
<div id=”” class=”field-required”>
<input id=”email” name=”email” class=”input-text” maxlength=”50″ name=”” value=”” />
</div>
<div id=”testid” class=”field-msg” style=”display:none;”><img src=”images/arrow_msg.gif” width=”13″ height=”13″/>Context help for email</div>
<div id=”” class=”field-msg-error” style=”display:none;”><img src=”images/arrow_msg_error.gif” width=”13″ height=”13″ style=””/>Error for email</div>
</div>
….
</form>
[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@cohq82authorApr 14.2009 — Can someone help?

What I am trying to do is to have my own error message DIV with display:none initially. If there is error, I want to run the fadein/fadeout to show that error.

The structure of the input/error DIV like this:

<div><input></input></div>

<div>context help</div>

<div>error msg</div>

The way this plugin works is to insert a LABEL right next to the INPUT and this is not what I want. I want to reuse the html code I have in the error msg DIV. I tried to look in errorPlacement but not sure how to use it. The goal is to insert/inject a jQuery function to do fadein/fadeout on a certain div when error occurs

Thanks
Copy linkTweet thisAlerts:
@Jeff_MottApr 14.2009 — In the docs, the error placement code looks like this:

errorPlacement: function(error, element) {
error.appendTo( element.parent("td").next("td") );
},


The biggest difference I see between this and your code is that you're not doing anything with the error. Your custom error DIV needs to not only fade in, but it probably needs the error message added to it as well.

Let me know if that helps any.
Copy linkTweet thisAlerts:
@cohq82authorApr 15.2009 — Actually I found my stupid error. It's fadeIn, NOT fadeOut..lol
Copy linkTweet thisAlerts:
@Phill_PaffordApr 17.2009 — You ever get this working? could I see the results and maybe some code to help out. Trying to do something of the same sorts.

Thanks in advance,

--Phill
Copy linkTweet thisAlerts:
@cohq82authorApr 19.2009 — Change this to fadein()

element.parent().next().next().fadeOut('fast');
×

Success!

Help @cohq82 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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