1
1
/*
2
- *
3
2
* FORM VALIDATION MADE EASY
4
3
* ------------------------------------------
5
4
* Created by Victor Jonsson <http://www.victorjonsson.se>
9
8
* (c) 2011 Victor Jonsson, Sweden.
10
9
* Dual licensed under the MIT or GPL Version 2 licenses
11
10
*
12
- * $version 1.2 beta
13
- *
11
+ * $version 1.2
14
12
*/
15
13
16
14
( function ( $ ) {
@@ -368,7 +366,7 @@ jQueryFormUtils.validateSwedishMobileNumber = function(number) {
368
366
* @return {Boolean }
369
367
*/
370
368
jQueryFormUtils . validateBirthdate = function ( val , dateFormat ) {
371
- var inputDate = this . validateDate ( val , dateFormat ) ;
369
+ var inputDate = this . parseDate ( val , dateFormat ) ;
372
370
if ( ! inputDate )
373
371
return false ;
374
372
@@ -392,29 +390,28 @@ jQueryFormUtils.validateBirthdate = function(val, dateFormat) {
392
390
} ;
393
391
394
392
/**
395
- * Is it a correct date YYYY-MM-DD . Will return false if not, otherwise
393
+ * Is it a correct date according to given dateFormat . Will return false if not, otherwise
396
394
* an array 0=>year 1=>month 2=>day
397
395
* @param {String } val
396
+ * @param {String } dateFormat
398
397
* @return {Array }|{Boolean}
399
398
*/
400
- jQueryFormUtils . validateDate = function ( val , dateFormat ) {
399
+ jQueryFormUtils . parseDate = function ( val , dateFormat ) {
401
400
var divider = dateFormat . replace ( / [ a - z A - Z ] / gi, '' ) . substring ( 0 , 1 ) ;
402
401
var regexp = '^' ;
403
402
var formatParts = dateFormat . split ( divider ) ;
404
403
for ( var i = 0 ; i < formatParts . length ; i ++ )
405
404
regexp += ( i > 0 ? '\\' + divider :'' ) + '(\\d{' + formatParts [ i ] . length + '})' ;
406
405
regexp += '$' ;
407
-
406
+
408
407
var matches = val . match ( new RegExp ( regexp ) ) ;
409
-
410
- // enklast m�jliga...
411
408
if ( matches == null )
412
409
return false ;
413
410
414
411
var findDateUnit = function ( unit , formatParts , matches ) {
415
412
for ( var i = 0 ; i < formatParts . length ; i ++ ) {
416
413
if ( formatParts [ i ] . substring ( 0 , 1 ) == unit ) {
417
- return matches [ i + 1 ] ;
414
+ return jQueryFormUtils . parseDateInt ( matches [ i + 1 ] ) ;
418
415
}
419
416
}
420
417
return - 1 ;
@@ -423,10 +420,7 @@ jQueryFormUtils.validateDate = function(val, dateFormat) {
423
420
var month = findDateUnit ( 'm' , formatParts , matches ) ;
424
421
var day = findDateUnit ( 'd' , formatParts , matches ) ;
425
422
var year = findDateUnit ( 'y' , formatParts , matches ) ;
426
- month = jQueryFormUtils . parseDateInt ( month ) ;
427
- day = jQueryFormUtils . parseDateInt ( day ) ;
428
- year = jQueryFormUtils . parseDateInt ( year ) ;
429
-
423
+
430
424
if ( month == 2 && day > 28 || month > 12 || month == 0 )
431
425
return false ;
432
426
if ( ( this . isShortMonth ( month ) && day > 30 ) || ( ! this . isShortMonth ( month ) && day > 31 ) || day == 0 )
@@ -675,7 +669,7 @@ jQueryFormUtils.validateInput = function(el, language, config, form) {
675
669
}
676
670
677
671
// Date
678
- else if ( validationRules . indexOf ( 'validate_date' ) > - 1 && ! jQueryFormUtils . validateDate ( value , config . dateFormat ) ) {
672
+ else if ( validationRules . indexOf ( 'validate_date' ) > - 1 && ! jQueryFormUtils . parseDate ( value , config . dateFormat ) ) {
679
673
return language . badDate ;
680
674
}
681
675
0 commit comments