Skip to content

Commit 163ed55

Browse files
committed
Fix for issue victorjonsson#113, started to work with the html5 module
1 parent d4cb285 commit 163ed55

14 files changed

+152
-18
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ it calls jQ func **$.formUtils.validateInput** to validate the single input when
294294

295295
## Changelog
296296

297+
#### 2.2.0 (unreleased)
298+
* Now possible to define an error message for each validation rule on a certain input (issue #113)
299+
* This plugin now serves as a html5 fallback. You can now use the native attributes to declare which type
300+
of validation that should be applied. (**Not yet implemented**)
301+
302+
297303
#### 2.1.47
298304
* Incorrect error-styling when using datepicker or suggestions is now fixed
299305
* Incorrect error-styling of select elements is now fixed

form-validator/date.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/#location-validators
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.1.47
13+
* @version 2.1.48
1414
*/
1515
(function($) {
1616

form-validator/file.dev.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.1.47
13+
* @version 2.1.48
1414
*/
1515
(function($, window) {
1616

@@ -45,6 +45,7 @@
4545
$.each(files, function(i, file) {
4646
valid = false;
4747
mime = file.type || '';
48+
alert(mime);
4849
$.each(allowedTypes, function(j, type) {
4950
valid = mime.indexOf(type) > -1;
5051
if( valid ) {

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/form-test.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@
317317

318318
$('#text-area').restrictLength($('#max-len'));
319319

320-
window.applyValidation(true, '#form-a', 'element');
320+
window.applyValidation(true, '#form-a', 'top');
321321
window.applyValidation(false, '#form-b', 'element');
322322
window.applyValidation(true, '#form-c', $('#error-container'));
323323

form-validator/html5.dev.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* jQuery Form Validator Module: html5
3+
* ------------------------------------------
4+
* Created by Victor Jonsson <http://www.victorjonsson.se>
5+
*
6+
* The following module will make this jQuery plugin serve as a
7+
* html5 fallback. It makes older browsers support the following
8+
* - validation when type="email"
9+
* - validation when type="url"
10+
* - validation when type="number" and max="" min=""
11+
* - validation when pattern="REGEXP"
12+
* - placeholders
13+
*
14+
* @website http://formvalidator.net/
15+
* @license Dual licensed under the MIT or GPL Version 2 licenses
16+
* @version 2.1.48
17+
*/
18+
(function($, window) {
19+
20+
"use strict";
21+
22+
var SUPPORTS_PLACEHOLDER = 'placeholder' in document.createElement('input');
23+
24+
25+
$(window).bind('formValidationSetup', function(evt, $form) {
26+
var $formInputs = $form.find('input,textarea,select');
27+
28+
$formInputs.each(function() {
29+
var validation = [],
30+
$input = $(this),
31+
attrs = {};
32+
33+
switch ( $input.attr('type').toLowerCase() ) {
34+
case 'url':
35+
validation.push('url');
36+
break;
37+
case 'email':
38+
validation.push('email');
39+
break;
40+
case 'number':
41+
validation.push('number');
42+
var max = $input.attr('max'),
43+
min = $input.attr('min');
44+
if( min || max ) {
45+
if( !min )
46+
min = 0;
47+
if( !max )
48+
max = 9007199254740992; // js max int
49+
50+
attrs['data-validation-allowing'] = 'range['+min+';'+max+']';
51+
if( min.indexOf('-') === 0 || max.indexOf('-') === 0 ) {
52+
attrs['data-validation-allowing'] += ',negative';
53+
}
54+
if( min.indexOf('.') > -1 || max.indexOf('.') > -1 ) {
55+
attrs['data-validation-allowing'] += ',float';
56+
}
57+
}
58+
break;
59+
}
60+
61+
if( validation.length == 0 && $input.attr('required') ) {
62+
validation.push('required');
63+
}
64+
65+
if( $input.attr('pattern') ) {
66+
validation.push('custom');
67+
attrs['data-validation-regexp'] = $input.attr('pattern');
68+
}
69+
70+
if( validation.length ) {
71+
$input.attr('data-validation', validation.join(' '));
72+
$.each(attrs, function(attrName, attrVal) {
73+
$input.attr(attrName, attrVal);
74+
});
75+
}
76+
});
77+
78+
79+
if( !SUPPORTS_PLACEHOLDER ) {
80+
$formInputs.filter('input[placeholder]').each(function() {
81+
this.defaultValue = this.getAttribute('placeholder');
82+
$(this)
83+
.bind('focus', function() {
84+
if(this.value == this.defaultValue) {
85+
this.value = '';
86+
$(this).removeClass('showing-placeholder');
87+
}
88+
})
89+
.bind('blur', function() {
90+
if($.trim(this.value) == '') {
91+
this.value = this.defaultValue;
92+
$(this).addClass('showing-placeholder');
93+
}
94+
});
95+
});
96+
}
97+
98+
});
99+
100+
});

form-validator/html5.js

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

form-validator/jquery.form-validator.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @website http://formvalidator.net/
77
* @license Dual licensed under the MIT or GPL Version 2 licenses
8-
* @version 2.1.47
8+
* @version 2.1.48
99
*/
1010
(function($) {
1111

@@ -373,7 +373,6 @@
373373
messages += '<br />* ' + mess;
374374
});
375375

376-
// using div instead of P gives better control of css display properties
377376
$form.children().eq(0).before('<div class="' + conf.errorMessageClass + ' alert alert-danger">' + messages + '</div>');
378377
}
379378

@@ -486,6 +485,8 @@
486485

487486
var $form = $(formQuery);
488487

488+
$(window).trigger('formValidationSetup', [$form]);
489+
489490
// Remove all event listeners previously added
490491
$form.find('.has-help-txt')
491492
.unbind('focus.validation')
@@ -835,11 +836,14 @@
835836
}
836837

837838
if(!isValid) {
838-
validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute);
839+
validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute+'-'+rule.replace('validate_', ''));
839840
if( !validationErrorMsg ) {
840-
validationErrorMsg = language[validator.errorMessageKey];
841-
if( !validationErrorMsg )
842-
validationErrorMsg = validator.errorMessage;
841+
validationErrorMsg = $elem.attr(conf.validationErrorMsgAttribute);
842+
if( !validationErrorMsg ) {
843+
validationErrorMsg = language[validator.errorMessageKey];
844+
if( !validationErrorMsg )
845+
validationErrorMsg = validator.errorMessage;
846+
}
843847
}
844848
return false; // breaks the iteration
845849
}
@@ -1492,6 +1496,9 @@
14921496
}
14931497

14941498
if( decimalSeparator == ',' ) {
1499+
if( val.indexOf('.') > -1 ) {
1500+
return false;
1501+
}
14951502
// Fix for checking range with floats using ,
14961503
val = val.replace(',', '.');
14971504
}

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

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

form-validator/location.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* @website http://formvalidator.net/#location-validators
1212
* @license Dual licensed under the MIT or GPL Version 2 licenses
13-
* @version 2.1.47
13+
* @version 2.1.48
1414
*/
1515
(function($) {
1616

form-validator/qunit.html

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,25 @@
8181

8282
function runAllTests() {
8383

84+
/*
85+
* Test error message
86+
*/
87+
test('Error messages', function() {
88+
var $input = input('', {'':'required'}),
89+
mess = $.formUtils.validateInput($input, $.formUtils.LANG, $.formUtils.defaultConfig(), $form);
90+
91+
equal('You have not answered all required fields', mess, 'Incorrect message');
92+
93+
$input = input('--', {'':'required alphanumeric', 'error-msg-alphanumeric':'custom for alphanum'}),
94+
mess = $.formUtils.validateInput($input, $.formUtils.LANG, $.formUtils.defaultConfig(), $form);
95+
equal('custom for alphanum', mess, 'Incorrect message');
96+
97+
$input = input('sam', {'':'required alphanumeric length', length:'min10', 'error-msg-alphanumeric':'custom for alphanum'}),
98+
mess = $.formUtils.validateInput($input, $.formUtils.LANG, $.formUtils.defaultConfig(), $form);
99+
equal('You have given an answer shorter than 10 characters', mess, 'Incorrect message');
100+
101+
});
102+
84103
/*
85104
* E-MAIL VALIDATION
86105
*/
@@ -230,7 +249,7 @@
230249
{val:input('1.0234', {'allowing':'float,negative', '':'number'}), isValid:true},
231250
{val:input('1.0235', {'allowing':'float,negative,number', '':'number'}), isValid:true},
232251
{val:input('1.0236', {'allowing':'float,negative,number', 'decimal-separator':',', '':'number'}), isValid:false},
233-
//{val:input('1,023', {'allowing':'float,negative,number', 'decimal-separator':',', '':'number'}), isValid:true},
252+
{val:input('1,023', {'allowing':'float,negative,number', 'decimal-separator':',', '':'number'}), isValid:true},
234253
{val:'123', isValid:true},
235254
{val:input('12', {'allowing':'range[1;10]', '':'number'}), isValid:false},
236255
{val:input('-1', {'allowing':'range[1;10]', '':'number'}), isValid:false},

form-validator/security.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
* @website http://formvalidator.net/#security-validators
1414
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 2.1.47
15+
* @version 2.1.48
1616
*/
1717
(function($) {
1818

form-validator/sweden.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @website http://formvalidator.net/#swedish-validators
1515
* @license Dual licensed under the MIT or GPL Version 2 licenses
16-
* @version 2.1.47
16+
* @version 2.1.48
1717
*/
1818
(function($, window) {
1919

form-validator/uk.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @website http://formvalidator.net/#uk-validators
1111
* @license Dual licensed under the MIT or GPL Version 2 licenses
12-
* @version 2.1.47
12+
* @version 2.1.48
1313
*/
1414
$.formUtils.addValidator({
1515
name : 'ukvatnumber',

0 commit comments

Comments
 (0)