diff --git a/Definitions/jquery.validation-1.10.d.ts b/Definitions/jquery.validation-1.10.d.ts index bbea45b94..e81d50af1 100644 --- a/Definitions/jquery.validation-1.10.d.ts +++ b/Definitions/jquery.validation-1.10.d.ts @@ -3,8 +3,61 @@ // Definitions by: https://github.com/fdecampredon // Definitions: https://github.com/borisyankov/DefinitelyTyped + /// +interface ValidationOptions { + debug?: bool; + submitHandler?: Function; + invalidHandler?: Function; + ignore?: any; + rules?: any; + messages?: any; + groups?: any; + onsubmit?: bool; + onfocusout?: bool; + onkeyup?: bool; + onclick?: bool; + focusInvalid?: bool; + focusCleanup?: bool; + meta?: string; + errorClass?: string; + validClass?: string; + errorElement?: string; + wrapper?: string; + errorLabelContainer?: any; + errorContainer?: any; + showErrors?: Function; + errorPlacement?: Function; + success?: any; + highlight?: Function; + unhighlight?: Function; + ignoreTitle?: bool; +} + +interface Validator { + format(template: string, ...arguments: string[]): string; + form(): bool; + element(element: any): bool; + resetForm(): void; + showErrors(errors: any): void; + numberOfInvalids(): number; + setDefaults(defaults: ValidationOptions): void; + addMethod(name: string, method: (value: any, element: any, ...params: any[]) => any, message?: any): void; + addClassRules(rules: any): void; + addClassRules(name: string, rules: any): void; +} + +interface JQuery { + validate(options?: ValidationOptions): Validator; + valid(): bool; + rules(): any; + rules(methodName: string): any; + rules(methodName: string, rules: any): any; + removeAttrs(attributes: string): any; +} + interface JQueryStatic { - validate(); + format(template: string, ...arguments: string[]): string; + validator: Validator; } \ No newline at end of file diff --git a/README.md b/README.md index 939cd1c9e..500d8c2f6 100644 --- a/README.md +++ b/README.md @@ -25,10 +25,11 @@ Complete * [Humane.js](http://wavded.github.com/humane-js/) (by [John Vrbanac](https://github.com/jmvrbanac)) * [Impress.js](https://github.com/bartaz/impress.js) (by [Boris Yankov](https://github.com/borisyankov)) * [Jasmine](http://pivotal.github.com/jasmine/) (by [Boris Yankov](https://github.com/borisyankov)) -* [jQuery.Globalize](https://github.com/jquery/globalize) (by [Boris Yankov](https://github.com/borisyankov)) * [jQuery](http://jquery.com/) (from TypeScript samples) * [jQuery Mobile](http://jquerymobile.com) (by [Boris Yankov](https://github.com/borisyankov)) * [jQuery UI](http://jqueryui.com/) (by [Boris Yankov](https://github.com/borisyankov)) +* [jQuery.Globalize](https://github.com/jquery/globalize) (by [Boris Yankov](https://github.com/borisyankov)) +* [jQuery.Validation](http://bassistance.de/jquery-plugins/jquery-plugin-validation/) (by [Boris Yankov](https://github.com/borisyankov)) * [Knockback](http://kmalakoff.github.com/knockback/) (by [Marcel Binot](https://github.com/docgit)) * [Knockout.js](http://knockoutjs.com/) (by [Boris Yankov](https://github.com/borisyankov)) * [Knockout.Mapping](https://github.com/SteveSanderson/knockout.mapping) (by [Boris Yankov](https://github.com/borisyankov)) diff --git a/Tests/jquery.validation-tests.ts b/Tests/jquery.validation-tests.ts index dc2a49f54..b4e7fc1d1 100644 --- a/Tests/jquery.validation-tests.ts +++ b/Tests/jquery.validation-tests.ts @@ -1,3 +1,223 @@ +/// /// -$("#commentForm").validate(); \ No newline at end of file +function test_validate() { + $("#commentForm").validate(); + $(".selector").validate({ + debug: true + }); + $(".selector").validate({ + submitHandler: function (form) { + $(form).ajaxSubmit(); + } + }); + $(".selector").validate({ + invalidHandler: function (form, validator) { + var errors = validator.numberOfInvalids(); + if (errors) { + var message = errors == 1 + ? 'You missed 1 field. It has been highlighted' + : 'You missed ' + errors + ' fields. They have been highlighted'; + $("div.error span").html(message); + $("div.error").show(); + } else { + $("div.error").hide(); + } + } + }); + $("#myform").validate({ + ignore: ".ignore" + }); + $(".selector").validate({ + rules: { + name: "required", + email: { + required: true, + email: true + } + } + }); + $(".selector").validate({ + rules: { + name: { + required: true, + minlength: 2 + } + }, + messages: { + name: { + required: "We need your email address to contact you", + minlength: jQuery.format("At least {0} characters required!") + } + } + }); + $("#myform").validate({ + groups: { + username: "fname lname" + }, + errorPlacement: function (error, element) { + if (element.attr("name") == "fname" + || element.attr("name") == "lname") + error.insertAfter("#lastname"); + else + error.insertAfter(element); + }, + debug: true + }); + $(".selector").validate({ + onsubmit: false + }); + $(".selector").validate({ + onfocusout: false + }); + $(".selector").validate({ + onkeyup: false + }); + $(".selector").validate({ + onclick: false + }); + $(".selector").validate({ + focusInvalid: false + }); + $(".selector").validate({ + focusCleanup: true + }); + $("#myform").validate({ + meta: "validate", + submitHandler: function () { alert("Submitted!") } + }); + $(".selector").validate({ + errorClass: "invalid" + }); + $(".selector").validate({ + validClass: "success" + }); + $(".selector").validate({ + errorElement: "em" + }); + $(".selector").validate({ + wrapper: "li" + }); + $("#myform").validate({ + errorLabelContainer: "#messageBox", + wrapper: "li", + submitHandler: function () { alert("Submitted!") } + }); + $("#myform").validate({ + errorContainer: "#messageBox1, #messageBox2", + errorLabelContainer: "#messageBox1 ul", + wrapper: "li", debug: true, + submitHandler: function () { alert("Submitted!") } + }); + $(".selector").validate({ + showErrors: function (errorMap, errorList) { + $("#summary").html("Your form contains " + this.numberOfInvalids() + " errors, see details below."); + this.defaultShowErrors(); + } + }); + $("#myform").validate({ + errorPlacement: function (error, element) { + error.appendTo(element.parent("td").next("td")); + }, + debug: true + }); + $("#myform").validate({ + success: "valid", + submitHandler: function () { alert("Submitted!") } + }); + $("#myform").validate({ + success: function (label) { + label.addClass("valid").text("Ok!") + }, + submitHandler: function () { alert("Submitted!") } + }); + $(".selector").validate({ + highlight: function (element, errorClass) { + $(element).fadeOut(function () { + $(element).fadeIn(); + }); + } + }); + $(".selector").validate({ + highlight: function (element, errorClass, validClass) { + $(element).addClass(errorClass).removeClass(validClass); + $(element.form).find("label[for=" + element.id + "]") + .addClass(errorClass); + }, + unhighlight: function (element, errorClass, validClass) { + $(element).removeClass(errorClass).addClass(validClass); + $(element.form).find("label[for=" + element.id + "]") + .removeClass(errorClass); + } + }); + $(".selector").validate({ + ignoreTitle: true + }); +} + +function test_methods() { + $("#myform").validate(); + $("a.check").click(function () { + alert("Valid: " + $("#myform").valid()); + return false; + }); + $("#myinput").rules("add", { + required: true, + minlength: 2, + messages: { + required: "Required input", + minlength: jQuery.format("Please, at least {0} characters are necessary") + } + }); + $("#myinput").rules("remove"); + $("#myinput").rules("remove", "min max"); + $("#myinput").rules("add", { + required: true, + minlength: 2, + messages: { + required: "Required input", + minlength: jQuery.format("Please, at least {0} characters are necessary") + } + }); + $("#skip").click(function () { + var rules = $("#myinput").removeAttrs("min max"); + $("#myform").submit(); + $("#myinput").attr(rules); + }); + $("#myform").validate().form(); + $("#myform").validate().element("#myselect"); + var validator = $("#myform").validate(); + validator.resetForm(); + validator.showErrors({ "firstname": "I know that your firstname is Pete, Pete!" }); + $("#summary").text(validator.numberOfInvalids() + " field(s) are invalid"); + jQuery.validator.setDefaults({ + debug: true + }); + jQuery.validator.addMethod("domain", function (value, element) { + return this.optional(element) || /^http:\/\/mycorporatedomain.com/.test(value); + }, "Please specify the correct domain for your documents"); + jQuery.validator.addClassRules({ + name: { + required: true, + minlength: 2 + }, + zip: { + required: true, + digits: true, + minlength: 5, + maxlength: 5 + } + }); + jQuery.validator.addClassRules({ + name: { + required: true, + minlength: 2 + }, + zip: { + required: true, + digits: true, + minlength: 5, + maxlength: 5 + } + }); +}