Skip to content

Commit a6bce50

Browse files
committed
fixed issue victorjonsson#153 if-checked renamed to depends-on and works with any type of input
1 parent 799178e commit a6bce50

26 files changed

+68
-70
lines changed

form-validator/brazil.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.

form-validator/date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* JQUERY-FORM-VALIDATOR
33
*
4-
* @version 2.2.164
4+
* @version 2.2.186
55
* @website http://formvalidator.net/
66
* @author Victor Jonsson, http://victorjonsson.se
77
* @license MIT

form-validator/file.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.

form-validator/html5.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.

form-validator/jquery.form-validator.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

form-validator/jsconf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* JQUERY-FORM-VALIDATOR
33
*
4-
* @version 2.2.164
4+
* @version 2.2.186
55
* @website http://formvalidator.net/
66
* @author Victor Jonsson, http://victorjonsson.se
77
* @license MIT

form-validator/lang/cz.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.

form-validator/lang/de.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.

form-validator/lang/es.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.

form-validator/lang/fr.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.

form-validator/lang/it.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.

form-validator/lang/pl.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.

form-validator/lang/pt.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.

form-validator/lang/ro.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.

form-validator/lang/ru.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.

form-validator/lang/sv.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.

form-validator/location.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.

form-validator/sanitize.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.

form-validator/security.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.

form-validator/src/utils.js

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@
55

66
'use strict';
77

8-
var $win = $(window);
8+
var $win = $(window),
9+
getInputValue = function(query, $parent) {
10+
var $inputs = $parent ? $parent.find(query) : query;
11+
if ($inputs.length > 0 ) {
12+
var type = $inputs.eq(0).attr('type');
13+
if (type === 'radio' || type === 'checkbox') {
14+
return $inputs.filter(':checked').val();
15+
} else {
16+
return $inputs.val();
17+
}
18+
}
19+
return false;
20+
};
921

1022
$.formUtils = $.extend($.formUtils || {}, {
1123

@@ -240,51 +252,32 @@
240252
conf = conf || $.formUtils.defaultConfig();
241253
language = language || $.formUtils.LANG;
242254

243-
var value = $elem.val() || '',
255+
var value = getInputValue($elem),
244256
result = {isValid: true, shouldChangeDisplay:true, errorMsg:''},
245-
optional = $elem.valAttr('optional'),
246-
247-
// test if a checkbox forces this element to be validated
248-
validationDependsOnCheckedInput = false,
249-
validationDependentInputIsChecked = false,
250-
validateIfCheckedElement = false,
251-
252-
// get value of this element's attribute "... if-checked"
253-
validateIfCheckedElementName = $elem.valAttr('if-checked'),
254-
// get expected radio button value for "if-checked" optional validation
255-
validateIfCheckedElementValue = $elem.valAttr('if-checked-value');
256-
257+
inputIsOptional = $elem.valAttr('optional'),
258+
skipBecauseDependingInputIsEmpty = false,
259+
skipBecauseItsEmpty = !value && inputIsOptional === 'true',
260+
skipBecauseInputIsHidden = $elem.attr('disabled') || (!$elem.is(':visible') && !conf.validateHiddenInputs),
261+
validationDependsOn = $elem.valAttr('depends-on') || $elem.valAttr('if-checked');
257262

258-
if ($elem.attr('disabled') || (!$elem.is(':visible') && !conf.validateHiddenInputs)) {
263+
if (skipBecauseInputIsHidden) {
259264
result.shouldChangeDisplay = false;
260265
return result;
261266
}
262267

263-
// make sure we can proceed
264-
if (validateIfCheckedElementName != null) {
268+
// Whether or not this input should be validated depends on if another input has a value
269+
if (validationDependsOn) {
265270

266271
// Set the boolean telling us that the validation depends
267272
// on another input being checked
268-
validationDependsOnCheckedInput = true;
273+
var valueOfDependingInput = getInputValue('input[name="' + validationDependsOn + '"]', $form),
274+
requiredValueOfDependingInput = $elem.valAttr('depends-on-value') || $elem.valAttr('if-checked-value'),
275+
dependingInputHasRequiredValue = !requiredValueOfDependingInput || requiredValueOfDependingInput === valueOfDependingInput;
269276

270-
// select the checkbox type element in this form
271-
validateIfCheckedElement = $form.find('input[name="' + validateIfCheckedElementName + '"]');
272-
273-
// test if check input value
274-
if (validateIfCheckedElementValue != null) {
275-
validateIfCheckedElement.each(function(index, el) {
276-
// test if it's property "checked" is checked and value equals expected value
277-
if ($(el).prop('checked') && $(el).val() === validateIfCheckedElementValue) {
278-
validationDependentInputIsChecked = true;
279-
}
280-
});
281-
}
282-
else {
283-
// test if it's property "checked" is checked
284-
if (validateIfCheckedElement.prop('checked')) {
285-
// set value for validation checkpoint
286-
validationDependentInputIsChecked = true;
287-
}
277+
if (valueOfDependingInput && dependingInputHasRequiredValue) {
278+
skipBecauseDependingInputIsEmpty = false;
279+
} else {
280+
skipBecauseDependingInputIsEmpty = true;
288281
}
289282
}
290283

@@ -297,10 +290,7 @@
297290
// Therefore, we cannot distinguish (apart from hacks) between an empty input type="text" and one with a
298291
// value that can't be parsed by the browser.
299292

300-
// validation checkpoint
301-
// if empty AND optional attribute is present
302-
// OR depending on a checkbox being checked AND checkbox is checked, return true
303-
if ((!value && optional === 'true') || (validationDependsOnCheckedInput && !validationDependentInputIsChecked)) {
293+
if (skipBecauseItsEmpty || skipBecauseDependingInputIsEmpty) {
304294
result.shouldChangeDisplay = conf.addValidClassOnAll;
305295
return result;
306296
}
@@ -317,7 +307,7 @@
317307

318308
// Filter out specified characters
319309
var ignore = $elem.valAttr('ignore');
320-
if( ignore ) {
310+
if (ignore) {
321311
$.each(ignore.split(''), function(i, char) {
322312
value = value.replace(new RegExp('\\'+char), '');
323313
});

form-validator/sweden.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.

form-validator/toggleDisabled.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.

form-validator/uk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* JQUERY-FORM-VALIDATOR
33
*
4-
* @version 2.2.164
4+
* @version 2.2.186
55
* @website http://formvalidator.net/
66
* @author Victor Jonsson, http://victorjonsson.se
77
* @license MIT

formvalidator.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"validation",
88
"validator"
99
],
10-
"version": "2.2.164",
10+
"version": "2.2.186",
1111
"author": {
1212
"name": "Victor Jonsson",
1313
"url": "http://victorjonsson.se",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jquery-form-validator",
33
"description": "With this feature rich jQuery plugin it becomes easy to validate user input while keeping your HTML markup clean from javascript code. Even though this plugin has a wide range of validation functions it's designed to require as little bandwidth as possible. This is achieved by grouping together validation functions in \"modules\", making it possible for the programmer to load only those functions that's needed to validate a particular form.",
4-
"version": "2.2.164",
4+
"version": "2.2.186",
55
"main": "./form-validator/jquery.form-validator.min.js",
66
"keywords": [
77
"form",

test/form.html

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,21 @@
276276
data-sanitize="numberFormat" data-sanitize-number-format="0,0.0">
277277
</div>
278278
<div class="form-group">
279-
<label class="control-label">Alphanumeric (will only be validated if the checkbox is checked)</label>
279+
<label class="control-label">Alphanumeric (will only be validated if checkbox below is checked)</label>
280280
<input name="test2"
281281
data-validation="alphanumeric"
282-
data-validation-error-msg="Invalid..."
283-
data-validation-if-checked="checker" />
282+
data-validation-error-msg="Invalid above..."
283+
data-validation-depends-on="checker" />
284284
<br />
285-
<input type="checkbox" name="checker" />
285+
Checkbox <input type="checkbox" name="checker" value="99" />
286+
</div>
287+
<div class="form-group">
288+
<label class="control-label">Alphanumeric (will only be validated if checkbox above is checked)</label>
289+
<input name="test2"
290+
data-validation="alphanumeric"
291+
data-validation-error-msg="Invalid below..."
292+
data-validation-depends-on="checker"
293+
data-validation-depends-on-value="99" />
286294
</div>
287295
<div id="error-container">
288296

0 commit comments

Comments
 (0)