Skip to content

Commit eaf8dc2

Browse files
committed
added some comments, renamed functions
1 parent 9d5ed46 commit eaf8dc2

File tree

2 files changed

+79
-45
lines changed

2 files changed

+79
-45
lines changed

example.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
99

1010
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
11+
<script type="text/javascript" src="jquery.js"></script>
1112
<script type="text/javascript" src="jquery.formvalidator.js"></script>
1213

1314
<link href="style.css" type="text/css" rel="stylesheet"/>
@@ -164,7 +165,7 @@ <h2>Example of all features</h2>
164165
</div>
165166

166167
<h2>
167-
Customized example (no border color on error, different position on error messages)
168+
Customized example (no border color on error, different position on error messages, validate input value on blur)
168169
</h2>
169170
<div class="section">
170171
<form action="" id="custom_form">
@@ -180,7 +181,7 @@ <h2>
180181

181182
<p>
182183
<strong>Domain</strong> <em>validate_domain</em><br/>
183-
<input id="testing" name="3" data-help="Only top domain name" data-validation="validate_domain"/>
184+
<input id="testing" name="3" data-help="No protocol is needed" data-validation="validate_domain"/>
184185
</p>
185186

186187
<p>
@@ -190,13 +191,13 @@ <h2>
190191

191192
<p>
192193
<strong>When will you arrive</strong>
193-
<em>Buy checking this option you will have to give a valid value on the question below otherwise not</em>
194+
<em>The answer given on the question below will only be validated if this option is unchecked</em>
194195
<input type="checkbox" name="disable_hours" checked="checked"/>
195196
</p>
196197

197198
<p>
198199
<strong>Time HH:mm</strong> <em>validate_time</em><br/>
199-
<input name="6" data-validation="validate_time" name="hours"/>
200+
<input data-validation="validate_time" name="hours"/>
200201
</p>
201202

202203
<p>
@@ -215,18 +216,17 @@ <h2>
215216
.submit(function() {
216217
conf.ignore.length = 0;
217218

218-
if (!$('input[name=disable_hours]').is(':checked'))
219+
if ($('input[name=disable_hours]').is(':checked'))
219220
conf.ignore.push('hours');
221+
220222
if ($(this).validate(false, conf))
221223
alert('Valid!');
222224

223225
return false;
224226
})
225-
.find('textarea,input')
226-
.blur(function() {
227-
$(this).doValidate(false, conf);
228-
})
229-
.showHelp();
227+
.validateOnBlur(false, conf)
228+
.showHelpOnFocus();
229+
230230
</script>
231231

232232
</div>

jquery.formvalidator.js

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,59 @@
11
/*
2-
FORM VALIDATION MADE EASY
3-
------------------------------------------
4-
Created by Victor Jonsson <http://www.victorjonsson.se>
5-
Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
6-
Available for download on jQuery.com <http://plugins.jquery.com/project/jQueryFormValidtor/>
7-
8-
(c) 2011 Victor Jonsson, Sweden.
9-
Dual licensed under the MIT or GPL Version 2 licenses
10-
11-
$version 1.1
2+
*
3+
* FORM VALIDATION MADE EASY
4+
* ------------------------------------------
5+
* Created by Victor Jonsson <http://www.victorjonsson.se>
6+
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
7+
* Available for download at jQuery.com <http://plugins.jquery.com/project/jQueryFormValidtor/>
8+
*
9+
* (c) 2011 Victor Jonsson, Sweden.
10+
* Dual licensed under the MIT or GPL Version 2 licenses
11+
*
12+
* $version 1.1
13+
*
1214
*/
15+
1316
(function($) {
1417
$.extend($.fn, {
1518

1619
/**
17-
* @param attrName
18-
* @return jQuery
20+
* Should be called on the element containing the input elements
21+
* @param {Object} language Optional, will override jQueryFormUtils.LANG
22+
* @param {Object} settings Optional, will override the default settings
23+
* @return {jQuery}
24+
*/
25+
validateOnBlur : function(language, settings) {
26+
$(this).find('textarea,input').blur(function() {
27+
$(this).doValidate(language, settings);
28+
});
29+
30+
return $(this);
31+
},
32+
33+
/**
34+
* Should be called on the element containing the input elements.
35+
* <input data-help="The info that I want to display for the user when input is focused" ... />
36+
* @param {String} attrName Optional, default is data-help
37+
* @return {jQuery}
1938
*/
20-
showHelp : function(attrName) {
39+
showHelpOnFocus : function(attrName) {
2140
if(typeof attrName == 'undefined')
2241
attrName = 'data-help';
2342

24-
$(this).each(function() {
43+
$(this).find('textarea,input').each(function() {
2544
var help = $(this).attr(attrName);
2645
if(help) {
2746
$(this)
2847
.focus(function() {
29-
var span = $('<span />')
30-
.addClass('jquery_form_help')
31-
.text(help)
32-
.hide()
33-
.fadeIn();
34-
35-
$(this).after(span);
48+
if($(this).parent().find('.jquery_form_help').length == 0) {
49+
$(this).after(
50+
$('<span />')
51+
.addClass('jquery_form_help')
52+
.text(help)
53+
.hide()
54+
.fadeIn()
55+
);
56+
}
3657
})
3758
.blur(function() {
3859
$(this).parent().find('.jquery_form_help')
@@ -42,18 +63,23 @@
4263
});
4364
}
4465
});
66+
4567
return $(this);
4668
},
4769

4870
/**
4971
* Function that validates the value of given input and shows
5072
* error message in a span element that is appended to the parent
5173
* element
52-
* @param language
53-
* @param settings
54-
* @return jQuery
74+
* @param {Object} language Optional, will override jQueryFormUtils.LANG
75+
* @param {Object} settings Optional, will override the default settings
76+
* @param {Boolean} attachKeyupEvent Optional
77+
* @return {jQuery}
5578
*/
56-
doValidate : function(language, settings) {
79+
doValidate : function(language, settings, attachKeyupEvent) {
80+
if(typeof attachKeyupEvent == 'undefined')
81+
attachKeyupEvent = true;
82+
5783
var config = {
5884
validationRuleAttribute : 'data-validation',
5985
errorElementClass : 'error', // Class that will be put on elements which value is invalid
@@ -75,21 +101,28 @@
75101
.removeClass(config.errorElementClass)
76102
.parent()
77103
.find('.jquery_form_error_message').remove();
104+
105+
if(config.borderColorOnError != '')
106+
$(this).css('border-color', jQueryFormUtils.defaultBorderColor);
78107

79108
var validation = jQueryFormUtils.validateInput($(this), language, config.validationRuleAttribute);
80109

81-
if(validation !== true) {
110+
if(validation === true)
111+
$(this).unbind('keyup');
112+
else {
82113
$(this)
83114
.addClass(config.errorElementClass)
84115
.parent()
85116
.append('<span class="jquery_form_error_message">'+validation+'</span>');
86117

87118
if(config.borderColorOnError != '')
88119
$(this).css('border-color', config.borderColorOnError);
89-
}
90-
else {
91-
if(config.borderColorOnError != '')
92-
$(this).css('border-color', jQueryFormUtils.defaultBorderColor);
120+
121+
if(attachKeyupEvent) {
122+
$(this).bind('keyup', function() {
123+
$(this).doValidate(language, settings, false);
124+
});
125+
}
93126
}
94127

95128
return $(this);
@@ -128,13 +161,14 @@
128161

129162
/**
130163
* Tells whether or not to validate element with this name and of this type
131-
* @param string name
132-
* @param string type
133-
* @return boolean
164+
* @param {String} name
165+
* @param {String} type
166+
* @return {Boolean}
134167
*/
135168
var ignoreInput = function(name, type) {
136-
if (type == 'submit')
169+
if (type == 'submit' || type == 'button')
137170
return true;
171+
138172
for (var i = 0; i < config.ignore.length; i++) {
139173
if (config.ignore[i] == name)
140174
return true;
@@ -237,7 +271,7 @@
237271
var messages = '<strong>' + language.errorTitle + '</strong>';
238272
for (var i = 0; i < errorMessages.length; i++)
239273
messages += '<br />* ' + errorMessages[i];
240-
$(this).children().eq(0).prepend('<p class="' + config.errorMessageClass + '">' + messages + '</p>');
274+
$(this).children().eq(0).before('<p class="' + config.errorMessageClass + '">' + messages + '</p>');
241275
if(config.scrollToTopOnError)
242276
$(window).scrollTop($(form).offset().top - 20);
243277
}

0 commit comments

Comments
 (0)