Skip to content

Commit 6e98cbf

Browse files
committed
added inline suggestions
1 parent d68b77f commit 6e98cbf

File tree

10 files changed

+130
-65
lines changed

10 files changed

+130
-65
lines changed

build

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
var fs = require('fs'),
44
sys = require('sys'),
55
exec = require('child_process').exec,
6-
jsPath = 'js/form-validator/',
7-
mainScript = jsPath + 'jquery.formvalidator.js',
6+
jsPath = 'form-validator/',
7+
mainScript = jsPath + 'jquery.form-validator.js',
88
newVersion = -1;
99

1010
/*
@@ -47,7 +47,7 @@ function buildFile(path, newName) {
4747
});
4848
}
4949

50-
buildFile(mainScript, jsPath + 'jquery.formvalidator.min.js');
50+
buildFile(mainScript, jsPath + 'jquery.form-validator.min.js');
5151
fs.readdirSync(jsPath).forEach(function(f) {
5252
if(f.substr(-7) == '.dev.js') {
5353
var compressedFileName = jsPath + f.substr(0, f.length - 6) + 'js';

form-validator/date.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*
1414
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 1.9.13
15+
* @version 1.9.15
1616
*/
1717
(function($) {
1818

form-validator/jquery.form-validator.js

Lines changed: 106 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
66
*
77
* @license Dual licensed under the MIT or GPL Version 2 licenses
8-
* @version 1.9.13
8+
* @version 1.9.15
99
*/
1010
(function($) {
1111

@@ -130,7 +130,7 @@
130130
validateWhenBlurred($element);
131131
}
132132

133-
var validation = $.formUtils.validateInput($element, language, config);
133+
var validation = $.formUtils.validateInput($element, language, config, $form);
134134

135135
if(validation === true) {
136136
$element.addClass('valid');
@@ -264,7 +264,8 @@
264264
var valid = $.formUtils.validateInput(
265265
$element,
266266
language,
267-
config
267+
config,
268+
$form
268269
);
269270

270271
if(valid !== true) {
@@ -298,7 +299,7 @@
298299
//
299300
// Remove possible error messages from last validation
300301
//
301-
$('.' + config.errorMessageClass.split(' ').join('.')).remove();
302+
$('.' + $.split(config.errorMessageClass, ' ').join('.')).remove();
302303
$('.jquery_form_error_message').remove();
303304

304305

@@ -354,6 +355,54 @@
354355
return this;
355356
};
356357

358+
/**
359+
* Add suggestion dropdown to inputs having data-suggestions with a comma
360+
* separated string with suggestions
361+
* @param {Array} [settings]
362+
* @returns {jQuery}
363+
*/
364+
$.fn.addSuggestions = function(settings) {
365+
this.find('input').each(function() {
366+
var $input = $(this),
367+
suggestions = $input.attr('data-suggestions');
368+
369+
if( suggestions && suggestions.length ) {
370+
$.formUtils.suggest($input, $.split(suggestions), settings);
371+
}
372+
});
373+
return this;
374+
};
375+
376+
/**
377+
* A bit smarter split function
378+
* @param {String} val
379+
* @param {Function|String} [func]
380+
* @param {String} [delim]
381+
* @returns {Array|void}
382+
*/
383+
$.split = function(val, func, delim) {
384+
if( typeof func != 'function' ) {
385+
// return string
386+
if( !val )
387+
return [];
388+
var values = [];
389+
$.each(val.split(func ? func:','), function(i,str) {
390+
str = $.trim(str);
391+
if( str.length )
392+
values.push(str);
393+
});
394+
return values;
395+
} else if( val ) {
396+
// use callback on each
397+
if( !delim )
398+
delim = ',';
399+
$.each(val.split(delim), function(i, str) {
400+
str = $.trim(str);
401+
if( str.length )
402+
return func(str, i);
403+
});
404+
}
405+
};
357406

358407
$.formUtils = {
359408

@@ -453,21 +502,22 @@
453502
loadModules : function(modules, path) {
454503

455504
var loadModuleScripts = function(modules, path) {
456-
var moduleList = modules.split(',');
457-
var numModules = moduleList.length;
458-
var moduleLoadedCallback = function() {
459-
numModules--;
460-
if( numModules == 0 ) {
461-
$.formUtils.trigger('load', path);
462-
}
463-
};
464-
$.each(moduleList, function(i, module) {
465-
var moduleName = $.trim(module);
466-
if( moduleName.length == 0 ) {
505+
var moduleList = $.split(modules),
506+
numModules = moduleList.length,
507+
moduleLoadedCallback = function() {
508+
numModules--;
509+
if( numModules == 0 ) {
510+
$.formUtils.trigger('load', path);
511+
}
512+
};
513+
514+
$.each(moduleList, function(i, modName) {
515+
modName = $.trim(modName);
516+
if( modName.length == 0 ) {
467517
moduleLoadedCallback();
468518
}
469519
else {
470-
var scriptUrl = path + $.trim(module) + '.js';
520+
var scriptUrl = path + modName + (modName.substr(-3) == '.js' ? '':'.js');
471521
$.ajax({
472522
url : scriptUrl,
473523
cache : scriptUrl.substr(-7) != '.dev.js',
@@ -478,7 +528,7 @@
478528
},
479529
error : function() {
480530
moduleLoadedCallback();
481-
throw new Error('Unable to load form validation module '+module);
531+
throw new Error('Unable to load form validation module '+modName);
482532
}
483533
});
484534
}
@@ -491,13 +541,15 @@
491541
$(function() {
492542
$('script').each(function() {
493543
var src = $(this).attr('src');
494-
var scriptName = src.substr(src.lastIndexOf('/')+1, src.length);
495-
if(scriptName == 'jquery.form-validator.js' || scriptName == 'jquery.form-validator.min.js') {
496-
path = src.substr(0, src.lastIndexOf('/')) + '/';
497-
if(path == '/')
498-
path = '';
544+
if( src ) {
545+
var scriptName = src.substr(src.lastIndexOf('/')+1, src.length);
546+
if(scriptName.indexOf('jquery.form-validator.js') > -1 || scriptName.indexOf('jquery.form-validator.min.js') > -1) {
547+
path = src.substr(0, src.lastIndexOf('/')) + '/';
548+
if(path == '/')
549+
path = '';
499550

500-
return false;
551+
return false;
552+
}
501553
}
502554
});
503555

@@ -514,9 +566,10 @@
514566
* @param {jQuery} $element
515567
* @param {Object} language ($.formUtils.LANG)
516568
* @param {Object} config
569+
* @param {jQuery} $form
517570
* @return {String|Boolean}
518571
*/
519-
validateInput : function($element, language, config) {
572+
validateInput : function($element, language, config, $form) {
520573

521574
// Multiple select
522575
if( $element.get(0).nodeName == 'SELECT' && $element.attr('multiple') ) {
@@ -560,37 +613,41 @@
560613
var validationRules = $element.attr(config.validationRuleAttribute);
561614

562615
// see if form element has inline err msg attribute
563-
var validationErrorMsg = $element.attr(config.validationErrorMsgAttribute);
616+
var validationErrorMsg = true;
564617

565-
if ( validationRules ) {
566-
var posRules = validationRules.split(' ');
567-
for(var i=0; i < posRules.length; i++) {
568-
if( posRules[i].substr(0, 9) != 'validate_' ) {
569-
posRules[i] = 'validate_' + posRules[i];
570-
}
618+
$.split(validationRules, function(rule) {
619+
if( rule.indexOf('validate_') !== 0 ) {
620+
rule = 'validate_' + rule;
621+
}
571622

572-
var validator = $.formUtils.validators[posRules[i]];
623+
var validator = $.formUtils.validators[rule];
573624

574-
if( validator && typeof validator['validate'] == 'function' ) {
625+
if( validator && typeof validator['validate'] == 'function' ) {
575626

576-
var isValid = validator.validate(value, $element, config, language, $element.closest('form'));
627+
var isValid = validator.validate(value, $element, config, language, $form);
577628

578-
if(!isValid) {
579-
$.formUtils.trigger('invalid', $element);
580-
if( !validationErrorMsg ) {
581-
validationErrorMsg = language[validator.errorMessageKey];
582-
if(typeof validationErrorMsg == 'undefined')
583-
validationErrorMsg = validator.errorMessage;
584-
}
585-
return validationErrorMsg;
629+
if(!isValid) {
630+
$.formUtils.trigger('invalid', $element);
631+
validationErrorMsg = $element.attr(config.validationErrorMsgAttribute);
632+
if( !validationErrorMsg ) {
633+
validationErrorMsg = language[validator.errorMessageKey];
634+
if( !validationErrorMsg )
635+
validationErrorMsg = validator.errorMessage;
586636
}
587-
} else {
588-
console.warn('Using undefined validator "'+posRules[i]+'"');
637+
return false; // breaks the iteration
589638
}
639+
} else {
640+
console.warn('Using undefined validator "'+rule+'"');
590641
}
642+
643+
}, ' ');
644+
645+
if( typeof validationErrorMsg == 'string' ) {
646+
return validationErrorMsg;
647+
} else {
648+
$.formUtils.trigger('valid', $element);
649+
return true;
591650
}
592-
$.formUtils.trigger('valid', $element);
593-
return true;
594651
},
595652

596653
/**
@@ -1074,8 +1131,8 @@
10741131
*/
10751132
$.formUtils.addValidator({
10761133
name : 'validate_required',
1077-
validate : function(val) {
1078-
return val !== '';
1134+
validate : function(val, $el) {
1135+
return $el.attr('type') == 'checkbox' ? $el.is(':checked') : $.trim(val) !== '';
10791136
},
10801137
errorMessage : '',
10811138
errorMessageKey: 'requiredFields'
@@ -1094,7 +1151,7 @@
10941151
return true;
10951152
}
10961153

1097-
var range = len.split('-');
1154+
var range = $.split(len, '-');
10981155

10991156
// range
11001157
if(range.length == 2 && (value.length < parseInt(range[0],10) || value.length > parseInt(range[1],10))) {

form-validator/jquery.form-validator.min.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/location.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* @license Dual licensed under the MIT or GPL Version 2 licenses
14-
* @version 1.9.13
14+
* @version 1.9.15
1515
*/
1616
(function($) {
1717

form-validator/security.dev.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* - validate_backend
1313
*
1414
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 1.9.13
15+
* @version 1.9.15
1616
*/
1717
(function($) {
1818

@@ -177,6 +177,7 @@
177177
}
178178

179179
$el.bind('keyup', function() {
180+
var val = $(this).val();
180181
var $parent = typeof config.parent == 'undefined' ? $(this).parent() : $(config.parent);
181182
var $displayContainer = $parent.find('.strength-meter');
182183
if($displayContainer.length == 0) {
@@ -186,7 +187,13 @@
186187
.appendTo($parent);
187188
}
188189

189-
var strength = $.formUtils.validators.validate_strength.calculatePasswordStrength($(this).val());
190+
if( !val ) {
191+
$displayContainer.hide();
192+
} else {
193+
$displayContainer.show();
194+
}
195+
196+
var strength = $.formUtils.validators.validate_strength.calculatePasswordStrength(val);
190197
var css = {
191198
background: 'pink',
192199
color : '#FF0000',
@@ -261,17 +268,17 @@
261268
url : backendUrl,
262269
type : 'POST',
263270
cache : false,
264-
data : 'validate='+val,
271+
data : $el.attr('name')+'='+val,
265272
dataType : 'json',
266-
success : function(json) {
273+
success : function(response) {
267274

268-
if(json.success) {
275+
if(response.valid) {
269276
$el.valAttr('backend-valid', 'true');
270277
}
271278
else {
272279
$el.valAttr('backend-invalid', 'true');
273-
if(json.message)
274-
$el.attr(conf.validationErrorMsgAttribute, json.message);
280+
if(response.message)
281+
$el.attr(conf.validationErrorMsgAttribute, response.message);
275282
else
276283
$el.removeAttr(conf.validationErrorMsgAttribute);
277284
}

0 commit comments

Comments
 (0)