Skip to content

Commit 45a769e

Browse files
committed
almost there...
1 parent cbb8e02 commit 45a769e

File tree

9 files changed

+73
-76
lines changed

9 files changed

+73
-76
lines changed

example.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h2>
3737
&lt;/form&gt;
3838
</code>
3939

40-
<h2>Example of all features</h2>
40+
<h2>Example of all default features</h2>
4141
<div class="section">
4242
<form action="" onsubmit="if($(this).validate()) alert('Valid!'); return false;">
4343
<p>

example/bootstrap.min.js

Lines changed: 0 additions & 6 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.

example/validate-email.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

form-validator/jquery.form-validator.js

Lines changed: 44 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
if($help.length == 0) {
5050
$help = $('<span />')
5151
.addClass(className)
52+
.addClass('form-help') // for css
5253
.text(help)
5354
.hide();
5455

@@ -74,44 +75,28 @@
7475
* error message in a span element that is appended to the parent
7576
* element
7677
*
77-
* @param {Object} language Optional, will override $.formUtils.LANG
78-
* @param {Object} settings Optional, will override the default settings
79-
* @param {Boolean} attachKeyupEvent Optional
78+
* @param {Object} [language] Optional, will override $.formUtils.LANG
79+
* @param {Object} [config] Optional, will override the default settings
80+
* @param {Boolean} [attachKeyupEvent] Optional
8081
* @return {jQuery}
8182
*/
82-
$.fn.doValidate = function(language, settings, attachKeyupEvent) {
83+
$.fn.doValidate = function(language, config, attachKeyupEvent) {
8384
if(typeof attachKeyupEvent == 'undefined') {
8485
attachKeyupEvent = true;
8586
}
8687

8788
var $element = this;
89+
8890
// test if there is custom obj to hold element error msg (id = element name + err_msg)
8991
var elementErrMsgObj = document.getElementById($element.attr('name')+'_err_msg');
9092

91-
var config = {
92-
ignore : [], // Names of inputs not to be validated, overwriting attribute notaed validation
93-
validationRuleAttribute : 'data-validation',
94-
validationErrorMsgAttribute : 'data-validation-error-msg', // define custom err msg inline with element
95-
errorElementClass : 'error', // Class that will be put on elements which value is invalid
96-
borderColorOnError : 'red',
97-
dateFormat : 'yyyy-mm-dd'
98-
};
99-
100-
if (settings) {
101-
$.extend(config, settings);
102-
}
103-
if (language) {
104-
$.extend($.formUtils.LANG, language);
105-
}
106-
// get updated dialog strings
107-
language = $.formUtils.LANG;
93+
language = $.extend(language || {}, $.formUtils.LANG);
94+
config = $.extend($.formUtils.defaultConfig(), config || {});
95+
config.errorMessagePosition = 'element';
10896

10997
var $form = $element.closest("form");
11098

111-
var elementType = $element.attr('type');
112-
if ($.formUtils.defaultBorderColor === null && elementType !== 'submit' && elementType !== 'checkbox' && elementType !== 'radio') {
113-
$.formUtils.defaultBorderColor = $element.css('border-color');
114-
}
99+
$.formUtils.figureOutDefaultBorderColor($element);
115100

116101
var validationRule = $element.attr(config.validationRuleAttribute);
117102

@@ -145,7 +130,7 @@
145130
validateWhenBlurred($element);
146131
}
147132

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

150135
if(validation === true) {
151136
$element.addClass('valid');
@@ -167,7 +152,7 @@
167152

168153
if(attachKeyupEvent) {
169154
$element.bind('keyup', function() {
170-
$(this).doValidate(language, settings, false);
155+
$(this).doValidate(language, config, false);
171156
});
172157
}
173158
}
@@ -197,18 +182,13 @@
197182
/**
198183
* Function that validate all inputs in a form
199184
*
200-
* @param language
201-
* @param config
185+
* @param [language]
186+
* @param [config]
202187
*/
203188
$.fn.validate = function(language, config) {
204189

205-
if(!config)
206-
config = {};
207-
if(!language)
208-
language = {};
209-
210-
$.extend(language, $.formUtils.LANG);
211-
$.extend(config, $.formUtils.defaultConfig);
190+
language = $.extend(language || {}, $.formUtils.LANG);
191+
config = $.extend($.formUtils.defaultConfig(), config || {});
212192

213193
/**
214194
* Adds message to error message stack if not already in the message stack
@@ -241,13 +221,7 @@
241221
if (type === 'submit' || type === 'button') {
242222
return true;
243223
}
244-
245-
for (var i = 0; i < config.ignore.length; i++) {
246-
if (config.ignore[i] === name) {
247-
return true;
248-
}
249-
}
250-
return false;
224+
return $.inArray(name, config.ignore || []) > -1;
251225
};
252226

253227
//
@@ -285,15 +259,12 @@
285259
else {
286260

287261
// memorize border color
288-
if ($.formUtils.defaultBorderColor === null && $element.attr('type')) {
289-
$.formUtils.defaultBorderColor = $element.css('border-color');
290-
}
262+
$.formUtils.figureOutDefaultBorderColor($element);
291263

292264
var valid = $.formUtils.validateInput(
293265
$element,
294266
language,
295-
config,
296-
$form
267+
config
297268
);
298269

299270
if(valid !== true) {
@@ -389,16 +360,28 @@
389360
/**
390361
* Default config for $(...).validate();
391362
*/
392-
defaultConfig : {
393-
ignore : [], // Names of inputs not to be validated even though node attribute containing the validation rules tells us to
394-
errorElementClass : 'error', // Class that will be put on elements which value is invalid
395-
borderColorOnError : 'red', // Border color of elements which value is invalid, empty string to not change border color
396-
errorMessageClass : 'jquery_form_error_message', // class name of div containing error messages when validation fails
397-
validationRuleAttribute : 'data-validation', // name of the attribute holding the validation rules
398-
validationErrorMsgAttribute : 'data-validation-error-msg', // define custom err msg inline with element
399-
errorMessagePosition : 'top', // Can be either "top" or "element"
400-
scrollToTopOnError : true,
401-
dateFormat : 'yyyy-mm-dd'
363+
defaultConfig : function() {
364+
return {
365+
ignore : [], // Names of inputs not to be validated even though node attribute containing the validation rules tells us to
366+
errorElementClass : 'error', // Class that will be put on elements which value is invalid
367+
borderColorOnError : 'red', // Border color of elements which value is invalid, empty string to not change border color
368+
errorMessageClass : 'jquery_form_error_message', // class name of div containing error messages when validation fails
369+
validationRuleAttribute : 'data-validation', // name of the attribute holding the validation rules
370+
validationErrorMsgAttribute : 'data-validation-error-msg', // define custom err msg inline with element
371+
errorMessagePosition : 'top', // Can be either "top" or "element"
372+
scrollToTopOnError : true,
373+
dateFormat : 'yyyy-mm-dd'
374+
}
375+
},
376+
377+
/**
378+
* @param {jQuery} $element
379+
*/
380+
figureOutDefaultBorderColor : function($element) {
381+
var elementType = $element.attr('type');
382+
if (this.defaultBorderColor === null && elementType !== 'submit' && elementType !== 'checkbox' && elementType !== 'radio') {
383+
this.defaultBorderColor = $element.css('border-color');
384+
}
402385
},
403386

404387
/**
@@ -531,20 +514,15 @@
531514
* @param {jQuery} $element
532515
* @param {Object} language ($.formUtils.LANG)
533516
* @param {Object} config
534-
* @param {jQuery} $form
535517
* @return {String|Boolean}
536518
*/
537-
validateInput : function($element, language, config, $form) {
519+
validateInput : function($element, language, config) {
538520

539521
// Multiple select
540522
if( $element.get(0).nodeName == 'SELECT' && $element.attr('multiple') ) {
541523
return this.validateMultipleSelect($element.val(), $element, config, language);
542524
}
543525

544-
// Form not given as argument
545-
if(!$form)
546-
$form = $element.closest('form');
547-
548526
var value = $.trim($element.val());
549527
value = value || '';
550528
var optional = $element.valAttr('optional');
@@ -592,12 +570,11 @@
592570
}
593571

594572
var validator = $.formUtils.validators[posRules[i]];
595-
console.log(posRules[i]);
596-
console.log(validator);
597573

598574
if( validator && typeof validator['validate'] == 'function' ) {
599575

600-
var isValid = validator.validate(value, $element, config, language, $form);
576+
var isValid = validator.validate(value, $element, config, language, $element.closest('form'));
577+
601578
if(!isValid) {
602579
$.formUtils.trigger('invalid', $element);
603580
if( !validationErrorMsg ) {

form-validator/sweden.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Validate swedish social security number yyyymmddXXXX
2222
*/
2323
$.formUtils.addValidator({
24-
name : 'validate_swesc',
24+
name : 'validate_swesec',
2525
validate : function(securityNumber) {
2626
if (!securityNumber.match(/^(\d{4})(\d{2})(\d{2})(\d{4})$/)) {
2727
return false;

formvalidator.jquery.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name" : "formvalidator",
3+
"title" : "jQuery Form Validator",
4+
"description" : "This plugin makes it 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.",
5+
"keywords": [
6+
"form",
7+
"validation",
8+
"validator"
9+
],
10+
"version" : "2.0.0",
11+
"author" : {
12+
"name": "Victor Jonsson",
13+
"url": "http://victorjonsson.se",
14+
"email" : "kontakt@victorjonsson.se"
15+
},
16+
"homepage" : "https://www.formvalidator.net",
17+
"demo" : "https://www.formvalidator.net",
18+
"download" : "https://raw.github.com/victorjonsson/jQuery-Form-Validator/master/jquery.formvalidator.min.js",
19+
"bugs" : "https://github.com/victorjonsson/jQuery-Form-Validator/issues",
20+
"licenses" : [{
21+
"type": "GPLv2",
22+
"url": "http://www.gnu.org/licenses/gpl-2.0.html"
23+
}],
24+
"dependencies" : {
25+
"jquery": ">=1.5"
26+
}
27+
}

0 commit comments

Comments
 (0)