Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit 387e9c7

Browse files
Adding support for checkbox, radio and multi-select; fixing scroll issue
1 parent b2eb93d commit 387e9c7

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

min/jquery.easy-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.easy-validation.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
**/
2828

29-
(function( $, document, window, UA ){
29+
(function( $, document, window, UA, UNDEFINED ){
3030

3131
var FALSE = false,
3232
TRUE = true,
@@ -36,7 +36,7 @@
3636
INVALID = ERROR + '-invalid',
3737
DEFAULT_EMPTY_MSG = 'This field cannot be left blank',
3838
DEFAULT_INVALID_MSG = 'This field is not properly formatted',
39-
container = 'form > ol > li, fieldset > ol > li, .form-item',
39+
container = 'form > ol > li, fieldset > ol > li',
4040

4141
// borrowed from Modernizr
4242
html5_validation = (function( props ){
@@ -112,7 +112,30 @@
112112
$container = $field.closest( container ),
113113
val = $field.val(),
114114
r, e;
115-
115+
116+
// skip already errored groups
117+
if ( $field.is(':checkbox,:radio') &&
118+
$container.hasClass( ERROR ) )
119+
{
120+
return;
121+
}
122+
123+
// additional value checks for multi-selects and radios & checkboxes
124+
switch ( TRUE )
125+
{
126+
case $field.is('select[multiple]'):
127+
val = $field.find('option:selected').val();
128+
break;
129+
case $field.is(':checkbox'):
130+
case $field.is(':radio'):
131+
val = $('[name=' + $field.attr('name') + ']:checked').val();
132+
break;
133+
}
134+
if ( val == UNDEFINED )
135+
{
136+
val = '';
137+
}
138+
116139
if ( $field.is('[required]') && val == '' )
117140
{
118141
e = $field.data( EMPTY ) || empty;
@@ -123,7 +146,7 @@
123146
$container.addClass( ERROR )
124147
);
125148
error = TRUE;
126-
if ( ! $first.length )
149+
if ( $first.length < 1 )
127150
{
128151
$first = $field;
129152
}
@@ -144,7 +167,7 @@
144167
$container.addClass( ERROR )
145168
);
146169
error = TRUE;
147-
if ( ! $first.length )
170+
if ( $first.length < 1 )
148171
{
149172
$first = $field;
150173
}
@@ -156,7 +179,7 @@
156179
// scroll to the first error
157180
if ( $first.length )
158181
{
159-
window.scrollTo( $first.offset().top );
182+
$(window).scrollTop( $first.offset().top );
160183
}
161184

162185
return ! error;

0 commit comments

Comments
 (0)