diff --git a/src/additional/cifES.js b/src/additional/cifES.js index e559730..1d94f8e 100644 --- a/src/additional/cifES.js +++ b/src/additional/cifES.js @@ -2,7 +2,7 @@ * Código de identificación fiscal ( CIF ) is the tax identification code for Spanish legal entities * Further rules can be found in Spanish on http://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal */ -jQuery.validator.addMethod( "cifES", function( value, element ) { +jQuery.validator.addMethod( "cifES", function( value ) { "use strict"; var sum, diff --git a/src/additional/currency.js b/src/additional/currency.js index 5a26fb1..3f25f33 100644 --- a/src/additional/currency.js +++ b/src/additional/currency.js @@ -27,9 +27,9 @@ * } */ jQuery.validator.addMethod("currency", function(value, element, param) { - var paramType = typeof param === "string", - symbol = paramType ? param : param[0], - soft = paramType ? true : param[1], + var isParamString = typeof param === "string", + symbol = isParamString ? param : param[0], + soft = isParamString ? true : param[1], regex; symbol = symbol.replace(/,/g, ""); diff --git a/src/additional/ipv4.js b/src/additional/ipv4.js index a6982aa..ea34a44 100644 --- a/src/additional/ipv4.js +++ b/src/additional/ipv4.js @@ -1,3 +1,3 @@ -jQuery.validator.addMethod("ipv4", function(value, element, param) { +jQuery.validator.addMethod("ipv4", function(value, element) { return this.optional(element) || /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/i.test(value); }, "Please enter a valid IP v4 address."); diff --git a/src/additional/ipv6.js b/src/additional/ipv6.js index f943ce1..85138d5 100644 --- a/src/additional/ipv6.js +++ b/src/additional/ipv6.js @@ -1,3 +1,3 @@ -jQuery.validator.addMethod("ipv6", function(value, element, param) { +jQuery.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); }, "Please enter a valid IP v6 address."); diff --git a/src/additional/nieES.js b/src/additional/nieES.js index 669ee82..66965a4 100644 --- a/src/additional/nieES.js +++ b/src/additional/nieES.js @@ -1,7 +1,7 @@ /* * The número de identidad de extranjero ( NIE )is a code used to identify the non-nationals in Spain */ -jQuery.validator.addMethod( "nieES", function( value, element ) { +jQuery.validator.addMethod( "nieES", function( value ) { "use strict"; value = value.toUpperCase(); diff --git a/src/additional/nifES.js b/src/additional/nifES.js index 14d497a..6fc4678 100644 --- a/src/additional/nifES.js +++ b/src/additional/nifES.js @@ -1,7 +1,7 @@ /* * The Número de Identificación Fiscal ( NIF ) is the way tax identification used in Spain for individuals */ -jQuery.validator.addMethod( "nifES", function( value, element ) { +jQuery.validator.addMethod( "nifES", function( value ) { "use strict"; value = value.toUpperCase(); diff --git a/src/additional/url2.js b/src/additional/url2.js index e2d630b..e7609b5 100644 --- a/src/additional/url2.js +++ b/src/additional/url2.js @@ -1,4 +1,4 @@ // same as url, but TLD is optional -jQuery.validator.addMethod("url2", function(value, element, param) { +jQuery.validator.addMethod("url2", function(value, element) { return this.optional(element) || /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value); }, jQuery.validator.messages.url); diff --git a/src/core.js b/src/core.js index 52aeb22..4277cf6 100644 --- a/src/core.js +++ b/src/core.js @@ -238,7 +238,7 @@ $.extend($.validator, { onsubmit: true, ignore: ":hidden", ignoreTitle: false, - onfocusin: function( element, event ) { + onfocusin: function( element ) { this.lastActive = element; // hide error label and remove error class on focus if enabled @@ -249,7 +249,7 @@ $.extend($.validator, { this.addWrapper(this.errorsFor(element)).hide(); } }, - onfocusout: function( element, event ) { + onfocusout: function( element ) { if ( !this.checkable(element) && (element.name in this.submitted || !this.optional(element)) ) { this.element(element); } @@ -261,7 +261,7 @@ $.extend($.validator, { this.element(element); } }, - onclick: function( element, event ) { + onclick: function( element ) { // click on selects, radiobuttons and checkboxes if ( element.name in this.submitted ) { this.element(element); @@ -462,6 +462,7 @@ $.extend($.validator, { }, objectLength: function( obj ) { + /* jshint unused: false */ var count = 0; for ( var i in obj ) { count++; @@ -795,7 +796,7 @@ $.extend($.validator, { }, dependTypes: { - "boolean": function( param, element ) { + "boolean": function( param ) { return param; }, "string": function( param, element ) { diff --git a/test/methods.js b/test/methods.js index 9028ab2..aba4181 100644 --- a/test/methods.js +++ b/test/methods.js @@ -386,7 +386,8 @@ test("remote", function() { test("remote, customized ajax options", function() { expect(2); stop(); - var v = $("#userForm").validate({ + + $("#userForm").validate({ rules: { username: { required: true, @@ -436,10 +437,12 @@ test("remote extensions", function() { }); $(document).ajaxStop(function() { $(document).unbind("ajaxStop"); - equal( 1, v.size(), "There must be one error" ); - equal( v.errorList[0].message, "asdf is already taken, please try something else" ); - v.element(e); - equal( v.errorList[0].message, "asdf is already taken, please try something else", "message doesn't change on revalidation" ); + if ( v.size() !== 0 ) { + ok( "There must be one error" ); + equal( v.errorList[0].message, "asdf is already taken, please try something else" ); + v.element(e); + equal( v.errorList[0].message, "asdf is already taken, please try something else", "message doesn't change on revalidation" ); + } start(); }); strictEqual( v.element(e), false, "invalid element, nothing entered yet" ); @@ -1168,13 +1171,13 @@ test("rangeWords", function(){ test("currency", function() { // Works with any symbol var method = methodTest( "currency" ); - ok( method( "£9", "£"), "Valid currency" ); - ok( method( "£9.9", "£"), "Valid currency" ); - ok( method( "£9.99", "£"), "Valid currency" ); + ok( method( "£9", "£"), "Symbol no decimal" ); + ok( method( "£9.9", "£"), "£, one decimal" ); + ok( method( "£9.99", "£"), "£, two decimal" ); ok( method( "£9.90", "£"), "Valid currency" ); - ok( method( "£9,999.9", "£"), "Valid currency" ); - ok( method( "£9,999.99", "£"), "Valid currency" ); - ok( method( "£9,999,999.9", "£"), "Valid currency" ); + ok( method( "£9,999.9", "£"), "£, thousand, comma separator, one decimal" ); + ok( method( "£9,999.99", "£"), "£, thousand, comma separator, two decimal" ); + ok( method( "£9,999,999.9", "£"), "£, million, comma separators, one decimal" ); ok( method( "9", ["£", false]), "Valid currency" ); ok( method( "9.9", ["£", false]), "Valid currency" ); ok( method( "9.99", ["£", false]), "Valid currency" ); diff --git a/test/rules.js b/test/rules.js index a093682..bc6ba24 100644 --- a/test/rules.js +++ b/test/rules.js @@ -2,39 +2,49 @@ module("rules"); test("rules() - internal - input", function() { var element = $("#firstname"); - var v = $("#testForm1").validate(); + + $("#testForm1").validate(); + deepEqual( element.rules(), { required: true, minlength: 2 } ); }); test("rules(), ignore method:false", function() { var element = $("#firstnamec"); - var v = $("#testForm1clean").validate({ + + $("#testForm1clean").validate({ rules: { firstname: { required: false, minlength: 2 } } }); + deepEqual( element.rules(), { minlength: 2 } ); }); test("rules() HTML5 required (no value)", function() { var element = $("#testForm11text1"); - var v = $("#testForm11").validate(); + + $("#testForm11").validate(); + deepEqual( element.rules(), { required: true } ); }); test("rules() - internal - select", function() { var element = $("#meal"); - var v = $("#testForm3").validate(); + + $("#testForm3").validate(); + deepEqual( element.rules(), {required: true} ); }); test("rules() - external", function() { var element = $("#text1"); - var v = $("#form").validate({ + + $("#form").validate({ rules: { action: {date: true, min: 5} } }); + deepEqual( element.rules(), {date: true, min: 5} ); }); @@ -61,13 +71,16 @@ test("rules() - external - complete form", function() { test("rules() - internal - input", function() { var element = $("#form8input"); - var v = $("#testForm8").validate(); + + $("#testForm8").validate(); + deepEqual( element.rules(), {required: true, number: true, rangelength: [2, 8]}); }); test("rules(), merge min/max to range, minlength/maxlength to rangelength", function() { jQuery.validator.autoCreateRanges = true; - var v = $("#testForm1clean").validate({ + + $("#testForm1clean").validate({ rules: { firstname: { min: 5, @@ -79,6 +92,7 @@ test("rules(), merge min/max to range, minlength/maxlength to rangelength", func } } }); + deepEqual( $("#firstnamec").rules(), {range: [5, 12]}); deepEqual( $("#lastnamec").rules(), {rangelength: [2, 8]} ); @@ -91,7 +105,7 @@ test("rules(), guarantee that required is at front", function() { $("#subformRequired").validate(); function flatRules(element) { var result = []; - jQuery.each($(element).rules(), function(key, value) { result.push(key); }); + jQuery.each($(element).rules(), function(key) { result.push(key); }); return result.join(" "); } equal( "required minlength", flatRules("#firstname") ); @@ -115,7 +129,8 @@ test("rules(), guarantee that required is at front", function() { test("rules(), evaluate dynamic parameters", function() { expect(2); - var v = $("#testForm1clean").validate({ + + $("#testForm1clean").validate({ rules: { firstname: { min: function(element) { @@ -125,6 +140,7 @@ test("rules(), evaluate dynamic parameters", function() { } } }); + deepEqual( $("#firstnamec").rules(), {min:12}); }); @@ -136,7 +152,8 @@ test("rules(), class and attribute combinations", function() { $.validator.addMethod("customMethod2", function() { return false; }, ""); - var v = $("#v2").validate({ + + $("#v2").validate({ rules: { "v2-i7": { required: true, @@ -145,6 +162,7 @@ test("rules(), class and attribute combinations", function() { } } }); + deepEqual( $("#v2-i1").rules(), { required: true }); deepEqual( $("#v2-i2").rules(), { required: true, email: true }); deepEqual( $("#v2-i3").rules(), { url: true }); @@ -215,11 +233,13 @@ test("rules(), add and remove", function() { }); test("rules(), add and remove static rules", function() { - var v = $("#testForm1clean").validate({ + + $("#testForm1clean").validate({ rules: { firstname: "required date" } }); + deepEqual( $("#firstnamec").rules(), { required: true, date: true } ); $("#firstnamec").rules("remove", "date"); diff --git a/test/test.js b/test/test.js index c802748..5569b12 100644 --- a/test/test.js +++ b/test/test.js @@ -20,7 +20,7 @@ $.mockjax({ $.mockjax({ url: "users2.php", data: { username: "asdf"}, - responseText: "'asdf is already taken, please try something else'", + responseText: "\"asdf is already taken, please try something else\"", responseStatus: 200, responseTime: 1 }); @@ -95,7 +95,7 @@ test("addMethod", function() { test("addMethod2", function() { expect( 4 ); - $.validator.addMethod("complicatedPassword", function(value, element, param) { + $.validator.addMethod("complicatedPassword", function(value, element) { return this.optional(element) || /\D/.test(value) && /\d/.test(value); }, "Your password must contain at least one number and one letter"); var v = jQuery("#form").validate({ @@ -103,8 +103,7 @@ test("addMethod2", function() { action: { complicatedPassword: true } } }); - var rule = $.validator.methods.complicatedPassword, - e = $("#text1")[0]; + var e = $("#text1")[0]; e.value = ""; strictEqual( v.element(e), true, "Rule is optional, valid" ); equal( 0, v.size() ); @@ -241,7 +240,6 @@ test("hide(): errorWrapper", function() { test("hide(): container", function() { expect(4); var errorLabel = $("#errorContainer"); - var element = $("#testForm3")[0]; var v = $("#testForm3").validate({ errorWrapper: "li", errorContainer: $("#errorContainer") }); v.form(); ok( errorLabel.is(":visible"), "Error label visible after validation" ); @@ -293,7 +291,6 @@ test("submitHandler keeps submitting button", function() { test("showErrors()", function() { expect( 4 ); var errorLabel = $("#errorFirstname").hide(); - var element = $("#firstname")[0]; var v = $("#testForm1").validate(); ok( errorLabel.is(":hidden") ); equal( 0, $("label.error[for=lastname]").size() ); @@ -618,7 +615,7 @@ test("error containers, with labelcontainer I", function() { equal( 2, labelcontainer.find("li").length, "There should be two error lis in the labelcontainer" ); ok( container.is(":visible"), "Check that the container is visible" ); ok( labelcontainer.is(":visible"), "Check that the labelcontainer is visible" ); - var labels = labelcontainer.find("label").each(function() { + labelcontainer.find("label").each(function() { ok( $(this).is(":visible"), "Check that each label is visible1" ); equal( "li", $(this).parent()[0].tagName.toLowerCase(), "Check that each label is wrapped in an li" ); ok( $(this).parent("li").is(":visible"), "Check that each parent li is visible" ); @@ -661,7 +658,7 @@ test("errorcontainer, show/hide only on submit", function() { test("option invalidHandler", function() { expect(1); - var v = $("#testForm1clean").validate({ + $("#testForm1clean").validate({ invalidHandler: function() { ok( true, "invalid-form event triggered called" ); start(); @@ -931,7 +928,7 @@ test("success is called for optional elements with other rules", function() { return true; }, ""); - var v = $("#testForm1clean").validate({ + $("#testForm1clean").validate({ success: function() { ok( true, "success called correctly!" ); }, @@ -1248,7 +1245,7 @@ test("correct checkbox receives the error", function(){ element.valid(); } var e1 = $("#check1").attr("checked", false); - var e2 = $("#check1b").attr("checked", false); + $("#check1b").attr("checked", false); var v = $("#form").find("[type=checkbox]").attr("checked", false).end().validate({ rules:{ check: { @@ -1332,7 +1329,8 @@ test("ignore hidden elements at start", function(){ test("Specify error messages through data attributes", function() { var form = $("#dataMessages"); var name = $("#dataMessagesName"); - var v = form.validate(); + + form.validate(); form.get(0).reset(); name.valid(); @@ -1345,10 +1343,10 @@ test("Updates pre-existing label if has error class", function() { var form = $("#updateLabel"), input = $("#updateLabelInput"), label = $("#targetLabel"), - v = form.validate(), labelsBefore = form.find("label").length, labelsAfter; + form.validate(); input.val(""); input.valid(); labelsAfter = form.find("label").length; @@ -1362,8 +1360,8 @@ test("Updates pre-existing label if has error class", function() { test("Min date set by attribute", function() { var form = $("#rangesMinDateInvalid"); var name = $("#minDateInvalid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1374,8 +1372,8 @@ test("Min date set by attribute", function() { test("Max date set by attribute", function() { var form = $("#ranges"); var name = $("#maxDateInvalid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1386,8 +1384,8 @@ test("Max date set by attribute", function() { test("Min and Max date set by attributes greater", function() { var form = $("#ranges"); var name = $("#rangeDateInvalidGreater"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1398,8 +1396,8 @@ test("Min and Max date set by attributes greater", function() { test("Min and Max date set by attributes less", function() { var form = $("#ranges"); var name = $("#rangeDateInvalidLess"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1410,8 +1408,8 @@ test("Min and Max date set by attributes less", function() { test("Min date set by attribute valid", function() { var form = $("#rangeMinDateValid"); var name = $("#minDateValid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1422,8 +1420,8 @@ test("Min date set by attribute valid", function() { test("Max date set by attribute valid", function() { var form = $("#ranges"); var name = $("#maxDateValid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1434,8 +1432,8 @@ test("Max date set by attribute valid", function() { test("Min and Max date set by attributes valid", function() { var form = $("#ranges"); var name = $("#rangeDateValid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1446,8 +1444,8 @@ test("Min and Max date set by attributes valid", function() { test("Min and Max strings set by attributes greater", function() { var form = $("#ranges"); var name = $("#rangeTextInvalidGreater"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1458,8 +1456,8 @@ test("Min and Max strings set by attributes greater", function() { test("Min and Max strings set by attributes less", function() { var form = $("#ranges"); var name = $("#rangeTextInvalidLess"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1470,8 +1468,8 @@ test("Min and Max strings set by attributes less", function() { test("Min and Max strings set by attributes valid", function() { var form = $("#ranges"); var name = $("#rangeTextValid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1498,8 +1496,8 @@ test( "calling blur on ignored element", function() { test("Min and Max type absent set by attributes greater", function() { var form = $("#ranges"); var name = $("#rangeAbsentInvalidGreater"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1510,8 +1508,8 @@ test("Min and Max type absent set by attributes greater", function() { test("Min and Max type absent set by attributes less", function() { var form = $("#ranges"); var name = $("#rangeAbsentInvalidLess"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1522,8 +1520,8 @@ test("Min and Max type absent set by attributes less", function() { test("Min and Max type absent set by attributes valid", function() { var form = $("#ranges"); var name = $("#rangeAbsentValid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1543,8 +1541,8 @@ test("Min and Max range set by attributes valid", function() { // var form = $("#ranges"); var name = $("#rangeRangeValid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1556,8 +1554,8 @@ test("Min and Max range set by attributes valid", function() { test("Min and Max number set by attributes valid", function() { var form = $("#ranges"); var name = $("#rangeNumberValid"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1569,8 +1567,8 @@ test("Min and Max number set by attributes valid", function() { test("Min and Max number set by attributes greater", function() { var form = $("#ranges"); var name = $("#rangeNumberInvalidGreater"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid(); @@ -1582,8 +1580,8 @@ test("Min and Max number set by attributes greater", function() { test("Min and Max number set by attributes less", function() { var form = $("#ranges"); var name = $("#rangeNumberInvalidLess"); - var v = form.validate(); + form.validate(); form.get(0).reset(); name.valid();