diff --git a/.gitignore b/.gitignore index 9e369c2..0fa49e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -_public/* -_old -demos/dev.html -build.sh -js/min.header.js +/node_modules/ + .DS_Store diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..2c40c44 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,15 @@ +{ + "curly": true, + "eqeqeq": true, + "immed": true, + "latedef": true, + "newcap": true, + "noarg": true, + "sub": true, + "undef": true, + "unused": true, + "boss": true, + "eqnull": true, + "node": true, + "es5": true +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e0ad853 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,31 @@ +# Contributing + +## Important notes +Please don't edit files in the `dist` subdirectory as they are generated via Grunt. You'll find source code in the `src` subdirectory! + +### Code style +Regarding code style like indentation and whitespace, **follow the conventions you see used in the source already.** + +### PhantomJS +While Grunt can run the included unit tests via [PhantomJS](http://phantomjs.org/), this shouldn't be considered a substitute for the real thing. Please be sure to test the `test/*.html` unit test file(s) in _actual_ browsers. + +## Modifying the code +First, ensure that you have the latest [Node.js](http://nodejs.org/) and [npm](http://npmjs.org/) installed. + +Test that Grunt's CLI is installed by running `grunt --version`. If the command isn't found, run `npm install -g grunt-cli`. For more information about installing Grunt, see the [getting started guide](http://gruntjs.com/getting-started). + +1. Fork and clone the repo. +1. Run `npm install` to install all dependencies (including Grunt). +1. Run `grunt` to grunt this project. + +Assuming that you don't see any red, you're ready to go. Just be sure to run `grunt` after making any changes, to ensure that nothing is broken. + +## Submitting pull requests + +1. Create a new branch, please don't work in your `master` branch directly. +1. Add failing tests for the change you want to make. Run `grunt` to see the tests fail. +1. Fix stuff. +1. Run `grunt` to see if the tests pass. Repeat steps 2-4 until done. +1. Open `test/*.html` unit test file(s) in actual browser to ensure tests pass everywhere. +1. Update the documentation to reflect any changes. +1. Push to your fork and submit a pull request. diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..98bae1e --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,109 @@ +'use strict'; + +module.exports = function (grunt) { + + // These plugins provide necessary tasks. + grunt.loadNpmTasks('grunt-contrib-clean'); + grunt.loadNpmTasks('grunt-contrib-concat'); + grunt.loadNpmTasks('grunt-contrib-uglify'); + grunt.loadNpmTasks('grunt-contrib-qunit'); + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-watch'); + grunt.loadNpmTasks('grunt-contrib-cssmin'); + + // Project configuration. + grunt.initConfig({ + // Metadata. + pkg: grunt.file.readJSON('package.json'), + banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' + + '<%= grunt.template.today("yyyy-mm-dd") %>\n' + + '<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' + + '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' + + ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n', + // Task configuration. + clean: { + files: ['dist'] + }, + cssmin: { + compress: { + options: { + banner: '<%= banner %>' + }, + files: { + "dist/uni-form.css": ["css/uni-form.css", "css/style.uni-form.css"] + } + }, + }, + concat: { + options: { + banner: '<%= banner %>', + stripBanners: true + }, + dist: { + src: ['src/uni-form.jquery.js'], + dest: 'dist/uni-form.jquery.js' + }, + dist_validation: { + src: ['src/uni-form-validation.jquery.js', 'src/validators/*.js'], + dest: 'dist/uni-form-validation.jquery.js' + } + }, + uglify: { + options: { + banner: '<%= banner %>' + }, + dist: { + src: '<%= concat.dist.dest %>', + dest: 'dist/uni-form.jquery.min.js' + }, + dist_validation: { + src: '<%= concat.dist_validation.dest %>', + dest: 'dist/uni-form-validation.jquery.min.js' + } + }, + qunit: { + files: ['test/**/*.html'] + }, + jshint: { + gruntfile: { + options: { + jshintrc: '.jshintrc' + }, + src: 'Gruntfile.js' + }, + src: { + options: { + jshintrc: 'src/.jshintrc' + }, + src: ['src/**/*.js'] + }, + test: { + options: { + jshintrc: 'test/.jshintrc' + }, + src: ['test/**/*.js'] + } + }, + watch: { + css: { + files: 'css/*.css', + tasks: ['cssmin'] + }, + src: { + files: '<%= jshint.src.src %>', + tasks: ['jshint:src', 'concat', 'uglify'] //, 'qunit'] + }, + test: { + files: '<%= jshint.test.src %>', + tasks: ['jshint:test', 'qunit'] + } + } + }); + + // Default task. + grunt.registerTask('default', ['jshint',/*'qunit', */ 'clean', 'concat', 'cssmin', 'uglify']); + + // Alias this to make it standard across my repos. (Jasmine v. QUnit) + grunt.registerTask('test', ['qunit']); + +}; diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..37b0a96 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,22 @@ +Copyright (c) 2013 Dragan Babic + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/demos/callback.html b/demos/callback.html index fe33239..58701a3 100644 --- a/demos/callback.html +++ b/demos/callback.html @@ -1,15 +1,14 @@ - - Uni–Form - + - + Uni–Form - + - + Uni–Form - - - - + + + + + Uni–Form - + @@ -50,39 +49,39 @@

- +

This is an example form data output demonstrating the read–only capabilities of Uni–Form

- +
- +

Default Style

Author: Dragan Babić see more styles →

- +

Looks like everything went OK! Check the data you entered and if everything is OK press the button to proceed.

- +

Default Form Field Layout

- +

Label of the input field

User–input value
- +

Label of the textarea

User–input value that contains a lot of text, like the output of a textarea that will have the text span multiple lines.
- +

Label of the select box

@@ -90,7 +89,7 @@

Label of the select box

Selected option's text
- +

Name of the checkbox group

@@ -98,7 +97,7 @@

Name of the checkbox group

Label of the selected checkbox
- +

Name of the radio group

@@ -106,7 +105,7 @@

Name of the radio group

Label of the selected radio button
- +

Name of the multifield group

@@ -122,22 +121,22 @@

Name of the multifield group

User–Input value
- +
- +

Alternate Form Fields Layout

- +

Label of the input field

User–input value
- +

Label of the textarea

User–input value that contains a lot of text, like the output of a textarea that will have the text span multiple lines.
- +

Label of the select box

@@ -145,7 +144,7 @@

Label of the select box

Selected option's text
- +

Name of the checkbox group

@@ -153,7 +152,7 @@

Name of the checkbox group

Label of the selected checkbox
- +

Name of the radio group

@@ -161,7 +160,7 @@

Name of the radio group

Label of the selected radio button
- +

Name of the multifield group

@@ -177,7 +176,7 @@

Name of the multifield group

User–Input value
- +
@@ -194,13 +193,13 @@

Name of the multifield group

- + diff --git a/demos/localization.html b/demos/localization.html index 439fd00..a889634 100644 --- a/demos/localization.html +++ b/demos/localization.html @@ -1,15 +1,14 @@ - - Uni–Form - + - + Uni–Form - + - + - - + + + + + + + - -

QUnit Test Suite

-

-
-

-
    +
    diff --git a/test/issues.js b/test/issues.js index 63c99b5..2332aad 100644 --- a/test/issues.js +++ b/test/issues.js @@ -2,23 +2,23 @@ module("Github cases"); /** * Case 1 - * + * * Prevent submit fails if the blur handler hasn't been called on data - * + * * @link https://github.com/LearningStation/uni-form/issues/issue/1 */ test("Case 1 : Prevent submit fails for existing data", function() { var $form = jQuery('#qunit-form'); - + jQuery('#email', $form).val('invalid@example'); - + $form.uniform({ prevent_submit : true }); $form.trigger('submit'); - - equals( + + equal( $form.hasClass('failedSubmit'), true, "Form has failedSubmit class after submit without blur on invalid data" @@ -28,23 +28,24 @@ test("Case 1 : Prevent submit fails for existing data", function() { /** * Case 2 - * + * * Feature request for required selection on radio button * when form inits with no selection - * + * * @link https://github.com/LearningStation/uni-form/issues/issue/2 */ test("Case 2 : Required validation for radio button", function() { var hasError = false, + $ = jQuery, $form = jQuery('#qunit-form'); - + $form .uniform({ invalid_class : 'invalid', error_class : 'error', prevent_submit : true, prevent_submit_callback : function($submit_form) { - hasError = + hasError = $('input[name="color"]:radio:first', $submit_form) .parents('div.ctrlHolder') .hasClass('error'); @@ -52,46 +53,46 @@ test("Case 2 : Required validation for radio button", function() { } }) .trigger('submit'); - - equals( + + equal( hasError, true, "Radio group has invalid class after submit" ); - + $('input[name="color"]:radio:first', $form).attr('checked', true); $form.trigger('submit'); - - equals( + + equal( hasError, false, "Radio group validated after selection and second submit" ); - + }); /** * Case 3 - * + * * data-default-value gets submitted - * + * * @link https://github.com/LearningStation/uni-form/issues/issue/3 */ test("Case 3 : data-default-value should not be submitted", function() { - var $form = jQuery('#qunit-form'); - + var $ = jQuery, $form = jQuery('#qunit-form'); + $form.uniform(); - equals( + equal( $('input[name="email"]', $form).val(), "Your email address", "Value of email field is same as prompt" ); - + $form.trigger('submit'); - equals( + equal( $('input[name="email"]', $form).val(), "", "Value of email field is empty after submit" @@ -101,22 +102,23 @@ test("Case 3 : data-default-value should not be submitted", function() { /** * Case 4 - * + * * Feature request for required selection on checkbox - * + * * @link https://github.com/LearningStation/uni-form/issues/issue/4 */ test("Case 4 : Required validation for checkbox", function() { - var hasError = false, + var $ = jQuery, + hasError = false, $form = jQuery('#qunit-form'); - + $form .uniform({ invalid_class : 'invalid', error_class : 'error', prevent_submit : true, prevent_submit_callback : function($submit_form) { - hasError = + hasError = $('input[name="agreement"]:checkbox', $submit_form) .parents('div.ctrlHolder') .hasClass('error'); @@ -124,55 +126,56 @@ test("Case 4 : Required validation for checkbox", function() { } }) .trigger('submit'); - - equals( + + equal( hasError, true, "Checkbox has invalid class after submit" ); - + $('input[name="agreement"]:checkbox', $form).attr('checked', true); $form.trigger('submit'); - - equals( + + equal( hasError, false, "Checkbox validated after selection and second submit" ); - + }); /** * Case 9 - * + * * Compatibility with HTML5 autofocus attribute - * + * * @link https://github.com/LearningStation/uni-form/issues/issue/15 */ test("Case 9 : Autofocus field works with highlight and default data", function() { - - var $input = $('#name'), + + var $ = jQuery, + $input = $('#name'), $form = jQuery('#qunit-form'); - + function supports_input_autofocus() { var i = document.createElement('input'); return 'autofocus' in i; } - + $input.attr('data-default-value', 'Sample data in case there is none'); - + $form.uniform({ focused_class : 'focused' }); - + // Branching structures in tests are bad, but unsure how to proceed here. if (supports_input_autofocus()) { // the ctrlHolder should be focused. - ok( + ok( $input.parents('div.ctrlHolder').hasClass('focused'), 'The autofocus form element should be highlighted.' ); - + // the default text should also be removed equal( $input.val(), @@ -186,30 +189,31 @@ test("Case 9 : Autofocus field works with highlight and default data", function( "This browser does not support autofocus" ); } - + }); /** * Case 15 - * + * * Default values may be rounding or being altered - * + * * @link https://github.com/LearningStation/uni-form/issues/issue/15 */ test("Case 15 : Default value with a period should not be rounded", function() { - var $input = $('#issue_15_a'), + var $ = jQuery, + $input = $('#issue_15_a'), $form = jQuery('#qunit-form'); - + $input.attr('data-default-value', '100.000'); $form.uniform(); - - equals( + + equal( $input.val(), $input.attr('data-default-value'), "A default value of 100.00 should display with decimal point intact." ); -}); \ No newline at end of file +}); diff --git a/test/validation.js b/test/validation.js index 9f6bc5c..dad13be 100644 --- a/test/validation.js +++ b/test/validation.js @@ -1,444 +1,456 @@ -// http://docs.jquery.com/Qunit - -/** - * Short accessor to a input element - * - * Note that IE can't do attr('type','text') - * - * @param string type input type enumerated [checkbox, radio, text, password] - * @param string value text value of input - * - * @return obj jQuery object - */ -var getInput = function(type, value) { - return $('').val(value); -} - -/** - * Test our validators - * - * Validators return: - * bool true for sucess - * string error message for failure - * - * So, this function tests for true/string, and makes the output - * in the unit test a little more clear - * - * @param mixed a result - * @param bool b expected pass/fail - * @param string message unit test message - * - * return bool result of assertion - */ -var validationTest = function(a, b, message) { - if (b === true) { - return ok((a === b), message); - } - return ok((typeof a == "string"), message); -} - - -module("Test fixtures"); - -test("Test data is correctly setup", function() { - ok(jQuery().uniform, "Plugin script not loaded/registered"); - ok(jQuery('#qunit-form').length, "Sample form present"); -}); - -var validators; -module("Validation unit tests", { - setup: function() { - validators = jQuery().uniform().validators; - } -}); - -test("Required test", function() { - var $input = getInput('text',''); - - validationTest( - validators.required($input.val('non empty text')), - true, - 'Required value detected' - ); - - validationTest( - validators.required($input.val('')), - false, - "Error message for empty input" - ); - -}); - -test("Minlength test", function() { - var $input = getInput('text',''); - $input - .addClass('validateMinLength') - .addClass('val-8'); - - validationTest( - validators.validateMinLength($input.val('non empty text')), // 14 char - true, - 'Minimum length passed with 14 char input' - ); - - validationTest( - validators.validateMinLength($input.val('Short')), - false, - "Error message set for short input" - ); - - validationTest( - validators.validateMinLength($input.val('')), - false, - "Error message set for empty input" - ); - -}); - - -test("Maxlength test", function() { - var $input = getInput('text',''); - $input - .addClass('validateMaxLength') - .addClass('val-8'); - - validationTest( - validators.validateMaxLength($input.val('non empty text')), // 14 char - false, - 'maximum length error with 14 char input' - ); - - validationTest( - validators.validateMaxLength($input.val('Short')), - true, - "Passed short input" - ); - - validationTest( - validators.validateMaxLength($input.val('')), - true, - "Passed empty input" - ); - -}); - -test("Min test", function() { - var $input = getInput('text',''); - $input - .addClass('validateMin') - .addClass('val-8'); - - validationTest( - validators.validateMin($input.val('6')), - false, - 'Value less than min' - ); - - validationTest( - validators.validateMin($input.val('8')), - true, - 'Value equal to min' - ); - - validationTest( - validators.validateMin($input.val('10')), - true, - "Value greater than min" - ); - -}); - -test("Max test", function() { - var $input = getInput('text',''); - $input - .addClass('validateMax') - .addClass('val-8'); - - validationTest( - validators.validateMax($input.val('6')), - true, - 'Value less than max' - ); - - validationTest( - validators.validateMax($input.val('8')), - true, - 'Value equal to max' - ); - - validationTest( - validators.validateMax($input.val('10')), - false, - "Value greater than max" - ); - -}); - -test("SameAs test", function() { - var $inputA = getInput('text','Same Value').attr('name','inputA'); - var $inputB = getInput('text','Same Value').attr('name','inputB').addClass('validateSameAs').addClass('inputA'); - - var $form = $('#qunit-fixture').append($inputA).append($inputB); - - validationTest( - validators.validateSameAs($inputB), - true, - 'Same Values' - ); - - $inputA.val('Different Value'); - validationTest( - validators.validateSameAs($inputB), - false, - "Different values" - ); - -}); - -test("Email address test", function() { - var $input = getInput('text',''), - address, - addresses = { - 'spam@example.com' : true, - 'spam@example.co.uk' : true, - 'spam.spam@example.co.uk' : true, - 'user+filter@gmail.com' : true, - 'spam@.com' : false, - 'spam@com' : false, - 'spam.com' : false - }, - explanation; - - for (address in addresses) { - explanation = (addresses[address]) ? ' passes' : ' fails' +(function($) { + /* + ======== A Handy Little QUnit Reference ======== + http://api.qunitjs.com/ + + Test methods: + module(name, {[setup][ ,teardown]}) + test(name, callback) + expect(numberOfAssertions) + stop(increment) + start(decrement) + Test assertions: + ok(value, [message]) + equal(actual, expected, [message]) + notEqual(actual, expected, [message]) + deepEqual(actual, expected, [message]) + notDeepEqual(actual, expected, [message]) + strictEqual(actual, expected, [message]) + notStrictEqual(actual, expected, [message]) + throws(block, [expected], [message]) + */ + + /** + * Short accessor to a input element + * + * Note that IE can't do attr('type','text') + * + * @param string type input type enumerated [checkbox, radio, text, password] + * @param string value text value of input + * + * @return obj jQuery object + */ + var getInput = function (type, value) { + return jQuery('').val(value); + }; + + /** + * Test our validators + * + * Validators return: + * bool true for sucess + * string error message for failure + * + * So, this function tests for true/string, and makes the output + * in the unit test a little more clear + * + * @param mixed a result + * @param bool b expected pass/fail + * @param string message unit test message + * + * return bool result of assertion + */ + var validationTest = function (a, b, message) { + if (b === true) { + return ok((a === b), message); + } + return ok((typeof a === "string"), message); + }; + + + module("Test fixtures", { + setup: function() { // This will run before each test in this module. + this.elems = $('#qunit-fixture').children(); + } + }); + + test("Test data is correctly setup", function () { + ok(jQuery().uniform, "Plugin script not loaded/registered"); + ok(jQuery('#qunit-form').length, "Sample form present"); + }); + + var validators; + module("Validation unit tests", { + setup: function () { + validators = jQuery.uniform.validators; + } + }); + + test("Required test", function () { + var $input = getInput('text',''); + + validationTest( + validators.required($input.val('non empty text')), + true, + 'Required value detected' + ); + + validationTest( + validators.required($input.val('')), + false, + "Error message for empty input" + ); + + }); + + test("Minlength test", function () { + var $input = getInput('text',''); + $input + .addClass('validateMinLength') + .addClass('val-8'); + + validationTest( + validators.validateMinLength($input.val('non empty text')), // 14 char + true, + 'Minimum length passed with 14 char input' + ); + + validationTest( + validators.validateMinLength($input.val('Short')), + false, + "Error message set for short input" + ); + validationTest( - validators.validateEmail($input.val(address)), - addresses[address], - address + explanation - ); - } - -}); - -test("Url test", function() { - var $input = getInput('text',''), - explanation, - address, - addresses = { - 'http://www.example.com/test/url' : true, - 'http://www.example.com/test.html' : true, - 'ftp://www.example.com' : true, - 'htp://www.example.com' : false, - 'http://www example.com' : false - }; - - for (address in addresses) { - explanation = (addresses[address]) ? ' passes' : ' fails' + validators.validateMinLength($input.val('')), + false, + "Error message set for empty input" + ); + + }); + + + test("Maxlength test", function () { + var $input = getInput('text',''); + $input + .addClass('validateMaxLength') + .addClass('val-8'); + validationTest( - validators.validateUrl($input.val(address)), - addresses[address], - address + explanation - ); - } - -}); - -test("Number test", function() { - var $input = getInput('text',''); - - validationTest( - validators.validateNumber($input.val('6')), - true, - 'Interger value' - ); - - validationTest( - validators.validateNumber($input.val('8.345')), - true, - 'Float value' - ); - - validationTest( - validators.validateNumber($input.val('Notanumber')), - false, - "Text" - ); - - validationTest( - validators.validateNumber($input.val('#$')), - false, - "Special characters" - ); - -}); - -test("Alpha test", function() { - var $input = getInput('text',''); - validationTest( - validators.validateAlpha($input.val('6')), - false, - 'Integer value' - ); - - validationTest( - validators.validateAlpha($input.val('8.345')), - false, - 'Float value' - ); - - validationTest( - validators.validateAlpha($input.val('Text with spaces')), - false, - "Text with spaces" - ); - - validationTest( - validators.validateAlpha($input.val('Word')), - true, - "Single word" - ); - - validationTest( - validators.validateAlpha($input.val('Word345')), - false, - "Single alphanum" - ); - -}); - -test("Alphanum test", function() { - var $input = getInput('text',''); - validationTest( - validators.validateAlphaNum($input.val('6')), - true, - 'Integer value' - ); - - // questionable, because "." is not in \W - validationTest( - validators.validateAlphaNum($input.val('8.345')), - false, - 'Float value' - ); - - validationTest( - validators.validateAlphaNum($input.val('Text with spaces')), - false, - "Text with spaces" - ); - - validationTest( - validators.validateAlphaNum($input.val('Word')), - true, - "Single word" - ); - - validationTest( - validators.validateAlphaNum($input.val('Word345')), - true, - "Single alphanum" - ); - -}); - -test("Phrase test", function() { - var $input = getInput('text',''); - - validationTest( - validators.validatePhrase($input.val('Text with spaces')), - true, - "Text with spaces" - ); - - validationTest( - validators.validatePhrase($input.val('Word')), - true, - "Single word" - ); - - validationTest( - validators.validatePhrase($input.val('Word345')), - true, - "Single alphanum" - ); - -}); - -test("Phone test", function() { - var $input = getInput('text',''), - numbers = { - '(308)-135-7895' : true, - '(123) 456-7890' : true, - '123-345-6789' : true, - '123 456-7890' : true, - '456-7890' : false, - '23456789' : false - }, - number, - explanation; - - for (number in numbers) { - explanation = (numbers[number]) ? ' passes' : ' fails' + validators.validateMaxLength($input.val('non empty text')), // 14 char + false, + 'maximum length error with 14 char input' + ); + validationTest( - validators.validatePhone($input.val(number)), - numbers[number], - number + explanation - ); - } - -}); - -test("Date test", function() { - var $input = getInput('text',''), - explanation, - date, - dates = { - '1/1/11' : true, - '1/1/2011' : true, - '1/1/2011' : true, - '11/1/2011' : true, - '01/01/2001' : true, // Case 6 - '16/40/2011' : false - }; - - for (date in dates) { - explanation = (dates[date]) ? ' passes' : ' fails' + validators.validateMaxLength($input.val('Short')), + true, + "Passed short input" + ); + validationTest( - validators.validateDate($input.val(date)), - dates[date], - date + explanation - ); - } - -}); - -test("Default data hides correctly", function() { - - var default_text = 'This is a sample', - $input = $('#issue_15_a'), - $form = jQuery('#qunit-form'); - - $input.attr('data-default-value', default_text); - - - $form.uniform(); - - // should be showing the default - equals( - $input.val(), - default_text, - "The default value has not been displayed correctly" - ); - - $input.focus(); - - // should now be empty - equals( - $input.val(), - '', - "The default value has not been displayed correctly" - ); - -}); + validators.validateMaxLength($input.val('')), + true, + "Passed empty input" + ); + + }); + + test("Min test", function () { + var $input = getInput('text',''); + $input + .addClass('validateMin') + .addClass('val-8'); + + validationTest( + validators.validateMin($input.val('6')), + false, + 'Value less than min' + ); + + validationTest( + validators.validateMin($input.val('8')), + true, + 'Value equal to min' + ); + + validationTest( + validators.validateMin($input.val('10')), + true, + "Value greater than min" + ); + + }); + + test("Max test", function () { + var $input = getInput('text',''); + $input + .addClass('validateMax') + .addClass('val-8'); + + validationTest( + validators.validateMax($input.val('6')), + true, + 'Value less than max' + ); + + validationTest( + validators.validateMax($input.val('8')), + true, + 'Value equal to max' + ); + + validationTest( + validators.validateMax($input.val('10')), + false, + "Value greater than max" + ); + + }); + + test("SameAs test", function () { + var $inputA = getInput('text', 'Same Value').attr('name','inputA'), + $inputB = getInput('text', 'Same Value').attr('name','inputB').addClass('validateSameAs').addClass('inputA'); + + $('#qunit-fixture').append($inputA).append($inputB); + + validationTest(validators.validateSameAs($inputB), true,'Same Values'); + + $inputA.val('Different Value'); + validationTest(validators.validateSameAs($inputB), false, "Different values"); + + }); + + test("Email address test", function () { + var $input = getInput('text',''), + address, + addresses = { + 'spam@example.com' : true, + 'spam@example.co.uk' : true, + 'spam.spam@example.co.uk' : true, + 'user+filter@gmail.com' : true, + 'spam@.com' : false, + 'spam@com' : false, + 'spam.com' : false + }, + explanation; + + for (address in addresses) { + explanation = (addresses[address]) ? ' passes': ' fails'; + validationTest( + validators.validateEmail($input.val(address)), + addresses[address], + address + explanation + ); + } + + }); + + test("Url test", function () { + var $input = getInput('text',''), + explanation, + address, + addresses = { + 'http://www.example.com/test/url' : true, + 'http://www.example.com/test.html' : true, + 'ftp://www.example.com' : true, + 'htp://www.example.com' : false, + 'http://www example.com' : false + }; + + for (address in addresses) { + explanation = (addresses[address]) ? ' passes': ' fails'; + validationTest( + validators.validateUrl($input.val(address)), + addresses[address], + address + explanation + ); + } + + }); + + test("Number test", function () { + var $input = getInput('text',''); + + validationTest( + validators.validateNumber($input.val('6')), + true, + 'Integer value' + ); + + validationTest( + validators.validateNumber($input.val('8.345')), + true, + 'Float value' + ); + + validationTest( + validators.validateNumber($input.val('Notanumber')), + false, + "Text" + ); + + validationTest( + validators.validateNumber($input.val('#$')), + false, + "Special characters" + ); + + }); + + test("Alpha test", function () { + var $input = getInput('text',''); + validationTest( + validators.validateAlpha($input.val('6')), + false, + 'Integer value' + ); + + validationTest( + validators.validateAlpha($input.val('8.345')), + false, + 'Float value' + ); + + validationTest( + validators.validateAlpha($input.val('Text with spaces')), + false, + "Text with spaces" + ); + + validationTest( + validators.validateAlpha($input.val('Word')), + true, + "Single word" + ); + + validationTest( + validators.validateAlpha($input.val('Word345')), + false, + "Single alphanum" + ); + + }); + + test("Alphanum test", function () { + var $input = getInput('text',''); + validationTest( + validators.validateAlphaNum($input.val('6')), + true, + 'Integer value' + ); + + // questionable, because "." is not in \W + validationTest( + validators.validateAlphaNum($input.val('8.345')), + false, + 'Float value' + ); + + validationTest( + validators.validateAlphaNum($input.val('Text with spaces')), + false, + "Text with spaces" + ); + + validationTest( + validators.validateAlphaNum($input.val('Word')), + true, + "Single word" + ); + + validationTest( + validators.validateAlphaNum($input.val('Word345')), + true, + "Single alphanum" + ); + + }); + + test("Phrase test", function () { + var $input = getInput('text',''); + + validationTest( + validators.validatePhrase($input.val('Text with spaces')), + true, + "Text with spaces" + ); + + validationTest( + validators.validatePhrase($input.val('Word')), + true, + "Single word" + ); + + validationTest( + validators.validatePhrase($input.val('Word345')), + true, + "Single alphanum" + ); + + }); + + test("Phone test", function () { + var $input = getInput('text',''), + numbers = { + '(308)-135-7895' : true, + '(123) 456-7890' : true, + '123-345-6789' : true, + '123 456-7890' : true, + '456-7890' : false, + '23456789' : false + }, + number, + explanation; + + for (number in numbers) { + explanation = (numbers[number]) ? ' passes' : ' fails'; + validationTest( + validators.validatePhone($input.val(number)), + numbers[number], + number + explanation + ); + } + + }); + + test("Date test", function () { + var $input = getInput('text',''), + explanation, + date, + dates = { + '1/1/11' : true, + '1/1/2011' : true, + '1/12/2011' : true, + '11/1/2011' : true, + '01/01/2001' : true, // Case 6 + '16/40/2011' : false + }; + + for (date in dates) { + explanation = (dates[date]) ? ' passes': ' fails'; + validationTest( + validators.validateDate($input.val(date)), + dates[date], + date + explanation + ); + } + + }); + + test("Default data hides correctly", function () { + + var default_text = 'This is a sample', + $input = $('#issue_15_a'), + $form = jQuery('#qunit-form'); + + $input.attr('data-default-value', default_text); + + $form.uniform(); + + // should be showing the default + equal($input.val(), default_text, + "The default value has not been displayed correctly" + ); + + $input.focus(); + + // should now be empty + equal($input.val(), '', + "The default value has not been displayed correctly" + ); + + }); +}(jQuery));