updates jquery validation to version 1.19.1

This commit is contained in:
2020-02-07 23:24:30 +01:00
parent 7701c94f8e
commit d80e270db2
4 changed files with 3134 additions and 2731 deletions

View File

@@ -1,9 +1,9 @@
/*! /*!
* jQuery Validation Plugin v1.17.0 * jQuery Validation Plugin v1.19.1
* *
* https://jqueryvalidation.org/ * https://jqueryvalidation.org/
* *
* Copyright (c) 2017 Jörn Zaefferer * Copyright (c) 2019 Jörn Zaefferer
* Released under the MIT license * Released under the MIT license
*/ */
(function( factory ) { (function( factory ) {
@@ -43,6 +43,38 @@
}() ); }() );
/**
* This is used in the United States to process payments, deposits,
* or transfers using the Automated Clearing House (ACH) or Fedwire
* systems. A very common use case would be to validate a form for
* an ACH bill payment.
*/
$.validator.addMethod( "abaRoutingNumber", function( value ) {
var checksum = 0;
var tokens = value.split( "" );
var length = tokens.length;
// Length Check
if ( length !== 9 ) {
return false;
}
// Calc the checksum
// https://en.wikipedia.org/wiki/ABA_routing_transit_number
for ( var i = 0; i < length; i += 3 ) {
checksum += parseInt( tokens[ i ], 10 ) * 3 +
parseInt( tokens[ i + 1 ], 10 ) * 7 +
parseInt( tokens[ i + 2 ], 10 );
}
// If not zero and divisible by 10 then valid
if ( checksum !== 0 && checksum % 10 === 0 ) {
return true;
}
return false;
}, "Please enter a valid routing number." );
// Accept a value from a file input based on a required mimetype // Accept a value from a file input based on a required mimetype
$.validator.addMethod( "accept", function( value, element, param ) { $.validator.addMethod( "accept", function( value, element, param ) {
@@ -256,11 +288,141 @@ $.validator.addMethod( "cifES", function( value, element ) {
}, "Please specify a valid CIF number." ); }, "Please specify a valid CIF number." );
/*
* Brazillian CNH number (Carteira Nacional de Habilitacao) is the License Driver number.
* CNH numbers have 11 digits in total: 9 numbers followed by 2 check numbers that are being used for validation.
*/
$.validator.addMethod( "cnhBR", function( value ) {
// Removing special characters from value
value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" );
// Checking value to have 11 digits only
if ( value.length !== 11 ) {
return false;
}
var sum = 0, dsc = 0, firstChar,
firstCN, secondCN, i, j, v;
firstChar = value.charAt( 0 );
if ( new Array( 12 ).join( firstChar ) === value ) {
return false;
}
// Step 1 - using first Check Number:
for ( i = 0, j = 9, v = 0; i < 9; ++i, --j ) {
sum += +( value.charAt( i ) * j );
}
firstCN = sum % 11;
if ( firstCN >= 10 ) {
firstCN = 0;
dsc = 2;
}
sum = 0;
for ( i = 0, j = 1, v = 0; i < 9; ++i, ++j ) {
sum += +( value.charAt( i ) * j );
}
secondCN = sum % 11;
if ( secondCN >= 10 ) {
secondCN = 0;
} else {
secondCN = secondCN - dsc;
}
return ( String( firstCN ).concat( secondCN ) === value.substr( -2 ) );
}, "Please specify a valid CNH number" );
/*
* Brazillian value number (Cadastrado de Pessoas Juridica).
* value numbers have 14 digits in total: 12 numbers followed by 2 check numbers that are being used for validation.
*/
$.validator.addMethod( "cnpjBR", function( value, element ) {
"use strict";
if ( this.optional( element ) ) {
return true;
}
// Removing no number
value = value.replace( /[^\d]+/g, "" );
// Checking value to have 14 digits only
if ( value.length !== 14 ) {
return false;
}
// Elimina values invalidos conhecidos
if ( value === "00000000000000" ||
value === "11111111111111" ||
value === "22222222222222" ||
value === "33333333333333" ||
value === "44444444444444" ||
value === "55555555555555" ||
value === "66666666666666" ||
value === "77777777777777" ||
value === "88888888888888" ||
value === "99999999999999" ) {
return false;
}
// Valida DVs
var tamanho = ( value.length - 2 );
var numeros = value.substring( 0, tamanho );
var digitos = value.substring( tamanho );
var soma = 0;
var pos = tamanho - 7;
for ( var i = tamanho; i >= 1; i-- ) {
soma += numeros.charAt( tamanho - i ) * pos--;
if ( pos < 2 ) {
pos = 9;
}
}
var resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if ( resultado !== parseInt( digitos.charAt( 0 ), 10 ) ) {
return false;
}
tamanho = tamanho + 1;
numeros = value.substring( 0, tamanho );
soma = 0;
pos = tamanho - 7;
for ( var il = tamanho; il >= 1; il-- ) {
soma += numeros.charAt( tamanho - il ) * pos--;
if ( pos < 2 ) {
pos = 9;
}
}
resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
if ( resultado !== parseInt( digitos.charAt( 1 ), 10 ) ) {
return false;
}
return true;
}, "Please specify a CNPJ value number" );
/* /*
* Brazillian CPF number (Cadastrado de Pessoas Físicas) is the equivalent of a Brazilian tax registration number. * Brazillian CPF number (Cadastrado de Pessoas Físicas) is the equivalent of a Brazilian tax registration number.
* CPF numbers have 11 digits in total: 9 numbers followed by 2 check numbers that are being used for validation. * CPF numbers have 11 digits in total: 9 numbers followed by 2 check numbers that are being used for validation.
*/ */
$.validator.addMethod( "cpfBR", function( value ) { $.validator.addMethod( "cpfBR", function( value, element ) {
"use strict";
if ( this.optional( element ) ) {
return true;
}
// Removing special characters from value // Removing special characters from value
value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" ); value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" );
@@ -337,7 +499,7 @@ $.validator.addMethod( "creditcard", function( value, element ) {
value = value.replace( /\D/g, "" ); value = value.replace( /\D/g, "" );
// Basing min and max length on // Basing min and max length on
// https://developer.ean.com/general_info/Valid_Credit_Card_Types // https://dev.ean.com/general-info/valid-card-types/
if ( value.length < 13 || value.length > 19 ) { if ( value.length < 13 || value.length > 19 ) {
return false; return false;
} }
@@ -359,7 +521,7 @@ $.validator.addMethod( "creditcard", function( value, element ) {
}, "Please enter a valid credit card number." ); }, "Please enter a valid credit card number." );
/* NOTICE: Modified version of Castle.Components.Validator.CreditCardValidator /* NOTICE: Modified version of Castle.Components.Validator.CreditCardValidator
* Redistributed under the the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0 * Redistributed under the Apache License 2.0 at http://www.apache.org/licenses/LICENSE-2.0
* Valid Types: mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown, all (overrides all other settings) * Valid Types: mastercard, visa, amex, dinersclub, enroute, discover, jcb, unknown, all (overrides all other settings)
*/ */
$.validator.addMethod( "creditcardtypes", function( value, element, param ) { $.validator.addMethod( "creditcardtypes", function( value, element, param ) {
@@ -398,7 +560,7 @@ $.validator.addMethod( "creditcardtypes", function( value, element, param ) {
if ( param.all ) { if ( param.all ) {
validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080; validTypes = 0x0001 | 0x0002 | 0x0004 | 0x0008 | 0x0010 | 0x0020 | 0x0040 | 0x0080;
} }
if ( validTypes & 0x0001 && /^(5[12345])/.test( value ) ) { // Mastercard if ( validTypes & 0x0001 && ( /^(5[12345])/.test( value ) || /^(2[234567])/.test( value ) ) ) { // Mastercard
return value.length === 16; return value.length === 16;
} }
if ( validTypes & 0x0002 && /^(4)/.test( value ) ) { // Visa if ( validTypes & 0x0002 && /^(4)/.test( value ) ) { // Visa
@@ -531,6 +693,30 @@ $.validator.addMethod( "giroaccountNL", function( value, element ) {
return this.optional( element ) || /^[0-9]{1,7}$/.test( value ); return this.optional( element ) || /^[0-9]{1,7}$/.test( value );
}, "Please specify a valid giro account number" ); }, "Please specify a valid giro account number" );
$.validator.addMethod( "greaterThan", function( value, element, param ) {
var target = $( param );
if ( this.settings.onfocusout && target.not( ".validate-greaterThan-blur" ).length ) {
target.addClass( "validate-greaterThan-blur" ).on( "blur.validate-greaterThan", function() {
$( element ).valid();
} );
}
return value > target.val();
}, "Please enter a greater value." );
$.validator.addMethod( "greaterThanEqual", function( value, element, param ) {
var target = $( param );
if ( this.settings.onfocusout && target.not( ".validate-greaterThanEqual-blur" ).length ) {
target.addClass( "validate-greaterThanEqual-blur" ).on( "blur.validate-greaterThanEqual", function() {
$( element ).valid();
} );
}
return value >= target.val();
}, "Please enter a greater value." );
/** /**
* IBAN is the international bank account number. * IBAN is the international bank account number.
* It has a country - specific format, that is checked here too * It has a country - specific format, that is checked here too
@@ -680,6 +866,30 @@ $.validator.addMethod( "ipv6", function( value, element ) {
return this.optional( element ) || /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/i.test( value ); return this.optional( element ) || /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/i.test( value );
}, "Please enter a valid IP v6 address." ); }, "Please enter a valid IP v6 address." );
$.validator.addMethod( "lessThan", function( value, element, param ) {
var target = $( param );
if ( this.settings.onfocusout && target.not( ".validate-lessThan-blur" ).length ) {
target.addClass( "validate-lessThan-blur" ).on( "blur.validate-lessThan", function() {
$( element ).valid();
} );
}
return value < target.val();
}, "Please enter a lesser value." );
$.validator.addMethod( "lessThanEqual", function( value, element, param ) {
var target = $( param );
if ( this.settings.onfocusout && target.not( ".validate-lessThanEqual-blur" ).length ) {
target.addClass( "validate-lessThanEqual-blur" ).on( "blur.validate-lessThanEqual", function() {
$( element ).valid();
} );
}
return value <= target.val();
}, "Please enter a lesser value." );
$.validator.addMethod( "lettersonly", function( value, element ) { $.validator.addMethod( "lettersonly", function( value, element ) {
return this.optional( element ) || /^[a-z]+$/i.test( value ); return this.optional( element ) || /^[a-z]+$/i.test( value );
}, "Letters only please" ); }, "Letters only please" );
@@ -688,10 +898,72 @@ $.validator.addMethod( "letterswithbasicpunc", function( value, element ) {
return this.optional( element ) || /^[a-z\-.,()'"\s]+$/i.test( value ); return this.optional( element ) || /^[a-z\-.,()'"\s]+$/i.test( value );
}, "Letters or punctuation only please" ); }, "Letters or punctuation only please" );
// Limit the number of files in a FileList.
$.validator.addMethod( "maxfiles", function( value, element, param ) {
if ( this.optional( element ) ) {
return true;
}
if ( $( element ).attr( "type" ) === "file" ) {
if ( element.files && element.files.length > param ) {
return false;
}
}
return true;
}, $.validator.format( "Please select no more than {0} files." ) );
// Limit the size of each individual file in a FileList.
$.validator.addMethod( "maxsize", function( value, element, param ) {
if ( this.optional( element ) ) {
return true;
}
if ( $( element ).attr( "type" ) === "file" ) {
if ( element.files && element.files.length ) {
for ( var i = 0; i < element.files.length; i++ ) {
if ( element.files[ i ].size > param ) {
return false;
}
}
}
}
return true;
}, $.validator.format( "File size must not exceed {0} bytes each." ) );
// Limit the size of all files in a FileList.
$.validator.addMethod( "maxsizetotal", function( value, element, param ) {
if ( this.optional( element ) ) {
return true;
}
if ( $( element ).attr( "type" ) === "file" ) {
if ( element.files && element.files.length ) {
var totalSize = 0;
for ( var i = 0; i < element.files.length; i++ ) {
totalSize += element.files[ i ].size;
if ( totalSize > param ) {
return false;
}
}
}
}
return true;
}, $.validator.format( "Total size of all files must not exceed {0} bytes." ) );
$.validator.addMethod( "mobileNL", function( value, element ) { $.validator.addMethod( "mobileNL", function( value, element ) {
return this.optional( element ) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)6((\s|\s?\-\s?)?[0-9]){8}$/.test( value ); return this.optional( element ) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)6((\s|\s?\-\s?)?[0-9]){8}$/.test( value );
}, "Please specify a valid mobile number" ); }, "Please specify a valid mobile number" );
$.validator.addMethod( "mobileRU", function( phone_number, element ) {
var ruPhone_number = phone_number.replace( /\(|\)|\s+|-/g, "" );
return this.optional( element ) || ruPhone_number.length > 9 && /^((\+7|7|8)+([0-9]){10})$/.test( ruPhone_number );
}, "Please specify a valid mobile number" );
/* For UK phone functions, do the following server side processing: /* For UK phone functions, do the following server side processing:
* Compare original input with this RegEx pattern: * Compare original input with this RegEx pattern:
* ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$ * ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$
@@ -804,6 +1076,64 @@ $.validator.addMethod( "nipPL", function( value ) {
return ( intControlNr === parseInt( value[ 9 ], 10 ) ); return ( intControlNr === parseInt( value[ 9 ], 10 ) );
}, "Please specify a valid NIP number." ); }, "Please specify a valid NIP number." );
/**
* Created for project jquery-validation.
* @Description Brazillian PIS or NIS number (Número de Identificação Social Pis ou Pasep) is the equivalent of a
* Brazilian tax registration number NIS of PIS numbers have 11 digits in total: 10 numbers followed by 1 check numbers
* that are being used for validation.
* @copyright (c) 21/08/2018 13:14, Cleiton da Silva Mendonça
* @author Cleiton da Silva Mendonça <cleiton.mendonca@gmail.com>
* @link http://gitlab.com/csmendonca Gitlab of Cleiton da Silva Mendonça
* @link http://github.com/csmendonca Github of Cleiton da Silva Mendonça
*/
$.validator.addMethod( "nisBR", function( value ) {
var number;
var cn;
var sum = 0;
var dv;
var count;
var multiplier;
// Removing special characters from value
value = value.replace( /([~!@#$%^&*()_+=`{}\[\]\-|\\:;'<>,.\/? ])+/g, "" );
// Checking value to have 11 digits only
if ( value.length !== 11 ) {
return false;
}
//Get check number of value
cn = parseInt( value.substring( 10, 11 ), 10 );
//Get number with 10 digits of the value
number = parseInt( value.substring( 0, 10 ), 10 );
for ( count = 2; count < 12; count++ ) {
multiplier = count;
if ( count === 10 ) {
multiplier = 2;
}
if ( count === 11 ) {
multiplier = 3;
}
sum += ( ( number % 10 ) * multiplier );
number = parseInt( number / 10, 10 );
}
dv = ( sum % 11 );
if ( dv > 1 ) {
dv = ( 11 - dv );
} else {
dv = 0;
}
if ( cn === dv ) {
return true;
} else {
return false;
}
}, "Please specify a valid NIS/PIS number" );
$.validator.addMethod( "notEqualTo", function( value, element, param ) { $.validator.addMethod( "notEqualTo", function( value, element, param ) {
return this.optional( element ) || !$.validator.methods.equalTo.call( this, value, element, param ); return this.optional( element ) || !$.validator.methods.equalTo.call( this, value, element, param );
}, "Please enter a different value, values must not be the same." ); }, "Please enter a different value, values must not be the same." );
@@ -842,6 +1172,30 @@ $.validator.addMethod( "phoneNL", function( value, element ) {
return this.optional( element ) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9]){8}$/.test( value ); return this.optional( element ) || /^((\+|00(\s|\s?\-\s?)?)31(\s|\s?\-\s?)?(\(0\)[\-\s]?)?|0)[1-9]((\s|\s?\-\s?)?[0-9]){8}$/.test( value );
}, "Please specify a valid phone number." ); }, "Please specify a valid phone number." );
/**
* Polish telephone numbers have 9 digits.
*
* Mobile phone numbers starts with following digits:
* 45, 50, 51, 53, 57, 60, 66, 69, 72, 73, 78, 79, 88.
*
* Fixed-line numbers starts with area codes:
* 12, 13, 14, 15, 16, 17, 18, 22, 23, 24, 25, 29, 32, 33,
* 34, 41, 42, 43, 44, 46, 48, 52, 54, 55, 56, 58, 59, 61,
* 62, 63, 65, 67, 68, 71, 74, 75, 76, 77, 81, 82, 83, 84,
* 85, 86, 87, 89, 91, 94, 95.
*
* Ministry of National Defence numbers and VoIP numbers starts with 26 and 39.
*
* Excludes intelligent networks (premium rate, shared cost, free phone numbers).
*
* Poland National Numbering Plan http://www.itu.int/oth/T02020000A8/en
*/
$.validator.addMethod( "phonePL", function( phone_number, element ) {
phone_number = phone_number.replace( /\s+/g, "" );
var regexp = /^(?:(?:(?:\+|00)?48)|(?:\(\+?48\)))?(?:1[2-8]|2[2-69]|3[2-49]|4[1-68]|5[0-9]|6[0-35-9]|[7-8][1-9]|9[145])\d{7}$/;
return this.optional( element ) || regexp.test( phone_number );
}, "Please specify a valid phone number" );
/* For UK phone functions, do the following server side processing: /* For UK phone functions, do the following server side processing:
* Compare original input with this RegEx pattern: * Compare original input with this RegEx pattern:
* ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$ * ^\(?(?:(?:00\)?[\s\-]?\(?|\+)(44)\)?[\s\-]?\(?(?:0\)?[\s\-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d\-]+)$
@@ -891,7 +1245,7 @@ $.validator.addMethod( "phoneUK", function( phone_number, element ) {
$.validator.addMethod( "phoneUS", function( phone_number, element ) { $.validator.addMethod( "phoneUS", function( phone_number, element ) {
phone_number = phone_number.replace( /\s+/g, "" ); phone_number = phone_number.replace( /\s+/g, "" );
return this.optional( element ) || phone_number.length > 9 && return this.optional( element ) || phone_number.length > 9 &&
phone_number.match( /^(\+?1-?)?(\([2-9]([02-9]\d|1[02-9])\)|[2-9]([02-9]\d|1[02-9]))-?[2-9]([02-9]\d|1[02-9])-?\d{4}$/ ); phone_number.match( /^(\+?1-?)?(\([2-9]([02-9]\d|1[02-9])\)|[2-9]([02-9]\d|1[02-9]))-?[2-9]\d{2}-?\d{4}$/ );
}, "Please specify a valid phone number" ); }, "Please specify a valid phone number" );
/* /*

File diff suppressed because one or more lines are too long

View File

@@ -1,9 +1,9 @@
/*! /*!
* jQuery Validation Plugin v1.17.0 * jQuery Validation Plugin v1.19.1
* *
* https://jqueryvalidation.org/ * https://jqueryvalidation.org/
* *
* Copyright (c) 2017 Jörn Zaefferer * Copyright (c) 2019 Jörn Zaefferer
* Released under the MIT license * Released under the MIT license
*/ */
(function( factory ) { (function( factory ) {
@@ -67,6 +67,7 @@ $.extend( $.fn, {
// Prevent form submit to be able to see console output // Prevent form submit to be able to see console output
event.preventDefault(); event.preventDefault();
} }
function handle() { function handle() {
var hidden, result; var hidden, result;
@@ -82,7 +83,7 @@ $.extend( $.fn, {
.appendTo( validator.currentForm ); .appendTo( validator.currentForm );
} }
if ( validator.settings.submitHandler ) { if ( validator.settings.submitHandler && !validator.settings.debug ) {
result = validator.settings.submitHandler.call( validator, validator.currentForm, event ); result = validator.settings.submitHandler.call( validator, validator.currentForm, event );
if ( hidden ) { if ( hidden ) {
@@ -142,6 +143,7 @@ $.extend( $.fn, {
// https://jqueryvalidation.org/rules/ // https://jqueryvalidation.org/rules/
rules: function( command, argument ) { rules: function( command, argument ) {
var element = this[ 0 ], var element = this[ 0 ],
isContentEditable = typeof this.attr( "contenteditable" ) !== "undefined" && this.attr( "contenteditable" ) !== "false",
settings, staticRules, existingRules, data, param, filtered; settings, staticRules, existingRules, data, param, filtered;
// If nothing is selected, return empty object; can't chain anyway // If nothing is selected, return empty object; can't chain anyway
@@ -149,7 +151,7 @@ $.extend( $.fn, {
return; return;
} }
if ( !element.form && element.hasAttribute( "contenteditable" ) ) { if ( !element.form && isContentEditable ) {
element.form = this.closest( "form" )[ 0 ]; element.form = this.closest( "form" )[ 0 ];
element.name = this.attr( "name" ); element.name = this.attr( "name" );
} }
@@ -393,7 +395,8 @@ $.extend( $.validator, {
this.invalid = {}; this.invalid = {};
this.reset(); this.reset();
var groups = ( this.groups = {} ), var currentForm = this.currentForm,
groups = ( this.groups = {} ),
rules; rules;
$.each( this.settings.groups, function( key, value ) { $.each( this.settings.groups, function( key, value ) {
if ( typeof value === "string" ) { if ( typeof value === "string" ) {
@@ -409,13 +412,20 @@ $.extend( $.validator, {
} ); } );
function delegate( event ) { function delegate( event ) {
var isContentEditable = typeof $( this ).attr( "contenteditable" ) !== "undefined" && $( this ).attr( "contenteditable" ) !== "false";
// Set form expando on contenteditable // Set form expando on contenteditable
if ( !this.form && this.hasAttribute( "contenteditable" ) ) { if ( !this.form && isContentEditable ) {
this.form = $( this ).closest( "form" )[ 0 ]; this.form = $( this ).closest( "form" )[ 0 ];
this.name = $( this ).attr( "name" ); this.name = $( this ).attr( "name" );
} }
// Ignore the element if it belongs to another form. This will happen mainly
// when setting the `form` attribute of an input to the id of another form.
if ( currentForm !== this.form ) {
return;
}
var validator = $.data( this.form, "validator" ), var validator = $.data( this.form, "validator" ),
eventType = "on" + event.type.replace( /^validate/, "" ), eventType = "on" + event.type.replace( /^validate/, "" ),
settings = validator.settings; settings = validator.settings;
@@ -610,7 +620,7 @@ $.extend( $.validator, {
try { try {
$( this.findLastActive() || this.errorList.length && this.errorList[ 0 ].element || [] ) $( this.findLastActive() || this.errorList.length && this.errorList[ 0 ].element || [] )
.filter( ":visible" ) .filter( ":visible" )
.focus() .trigger( "focus" )
// Manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find // Manually trigger focusin event; without it, focusin handler isn't called, findLastActive won't have anything to find
.trigger( "focusin" ); .trigger( "focusin" );
@@ -639,16 +649,23 @@ $.extend( $.validator, {
.not( this.settings.ignore ) .not( this.settings.ignore )
.filter( function() { .filter( function() {
var name = this.name || $( this ).attr( "name" ); // For contenteditable var name = this.name || $( this ).attr( "name" ); // For contenteditable
var isContentEditable = typeof $( this ).attr( "contenteditable" ) !== "undefined" && $( this ).attr( "contenteditable" ) !== "false";
if ( !name && validator.settings.debug && window.console ) { if ( !name && validator.settings.debug && window.console ) {
console.error( "%o has no name assigned", this ); console.error( "%o has no name assigned", this );
} }
// Set form expando on contenteditable // Set form expando on contenteditable
if ( this.hasAttribute( "contenteditable" ) ) { if ( isContentEditable ) {
this.form = $( this ).closest( "form" )[ 0 ]; this.form = $( this ).closest( "form" )[ 0 ];
this.name = name; this.name = name;
} }
// Ignore elements that belong to other/nested forms
if ( this.form !== validator.currentForm ) {
return false;
}
// Select only the first element for each name, and only those with rules specified // Select only the first element for each name, and only those with rules specified
if ( name in rulesCache || !validator.objectLength( $( this ).rules() ) ) { if ( name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
return false; return false;
@@ -694,6 +711,7 @@ $.extend( $.validator, {
elementValue: function( element ) { elementValue: function( element ) {
var $element = $( element ), var $element = $( element ),
type = element.type, type = element.type,
isContentEditable = typeof $element.attr( "contenteditable" ) !== "undefined" && $element.attr( "contenteditable" ) !== "false",
val, idx; val, idx;
if ( type === "radio" || type === "checkbox" ) { if ( type === "radio" || type === "checkbox" ) {
@@ -702,7 +720,7 @@ $.extend( $.validator, {
return element.validity.badInput ? "NaN" : $element.val(); return element.validity.badInput ? "NaN" : $element.val();
} }
if ( element.hasAttribute( "contenteditable" ) ) { if ( isContentEditable ) {
val = $element.text(); val = $element.text();
} else { } else {
val = $element.val(); val = $element.val();
@@ -763,10 +781,6 @@ $.extend( $.validator, {
if ( normalizer ) { if ( normalizer ) {
val = normalizer.call( element, val ); val = normalizer.call( element, val );
if ( typeof val !== "string" ) {
throw new TypeError( "The normalizer should return a string value." );
}
// Delete the normalizer from rules to avoid treating it as a pre-defined method. // Delete the normalizer from rules to avoid treating it as a pre-defined method.
delete rules.normalizer; delete rules.normalizer;
} }
@@ -1142,7 +1156,19 @@ $.extend( $.validator, {
.removeData( "validator" ) .removeData( "validator" )
.find( ".validate-equalTo-blur" ) .find( ".validate-equalTo-blur" )
.off( ".validate-equalTo" ) .off( ".validate-equalTo" )
.removeClass( "validate-equalTo-blur" ); .removeClass( "validate-equalTo-blur" )
.find( ".validate-lessThan-blur" )
.off( ".validate-lessThan" )
.removeClass( "validate-lessThan-blur" )
.find( ".validate-lessThanEqual-blur" )
.off( ".validate-lessThanEqual" )
.removeClass( "validate-lessThanEqual-blur" )
.find( ".validate-greaterThanEqual-blur" )
.off( ".validate-greaterThanEqual" )
.removeClass( "validate-greaterThanEqual-blur" )
.find( ".validate-greaterThan-blur" )
.off( ".validate-greaterThan" )
.removeClass( "validate-greaterThan-blur" );
} }
}, },
@@ -1246,6 +1272,12 @@ $.extend( $.validator, {
for ( method in $.validator.methods ) { for ( method in $.validator.methods ) {
value = $element.data( "rule" + method.charAt( 0 ).toUpperCase() + method.substring( 1 ).toLowerCase() ); value = $element.data( "rule" + method.charAt( 0 ).toUpperCase() + method.substring( 1 ).toLowerCase() );
// Cast empty attributes like `data-rule-required` to `true`
if ( value === "" ) {
value = true;
}
this.normalizeAttributeRule( rules, type, method, value ); this.normalizeAttributeRule( rules, type, method, value );
} }
return rules; return rules;
@@ -1371,7 +1403,7 @@ $.extend( $.validator, {
if ( this.checkable( element ) ) { if ( this.checkable( element ) ) {
return this.getLength( value, element ) > 0; return this.getLength( value, element ) > 0;
} }
return value.length > 0; return value !== undefined && value !== null && value.length > 0;
}, },
// https://jqueryvalidation.org/email-method/ // https://jqueryvalidation.org/email-method/
@@ -1395,9 +1427,26 @@ $.extend( $.validator, {
}, },
// https://jqueryvalidation.org/date-method/ // https://jqueryvalidation.org/date-method/
date: function( value, element ) { date: ( function() {
return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() ); var called = false;
},
return function( value, element ) {
if ( !called ) {
called = true;
if ( this.settings.debug && window.console ) {
console.warn(
"The `date` method is deprecated and will be removed in version '2.0.0'.\n" +
"Please don't use it, since it relies on the Date constructor, which\n" +
"behaves very differently across browsers and locales. Use `dateISO`\n" +
"instead or one of the locale specific methods in `localizations/`\n" +
"and `additional-methods.js`."
);
}
}
return this.optional( element ) || !/Invalid|NaN/.test( new Date( value ).toString() );
};
}() ),
// https://jqueryvalidation.org/dateISO-method/ // https://jqueryvalidation.org/dateISO-method/
dateISO: function( value, element ) { dateISO: function( value, element ) {

File diff suppressed because one or more lines are too long