From ed2945a30b75fec4963dac6c4e9993a7057234c8 Mon Sep 17 00:00:00 2001 From: Nicolas McCurdy Date: Sat, 10 Jan 2015 23:14:09 -0500 Subject: [PATCH 1/3] Nick's solution --- validator.js | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/validator.js b/validator.js index 860dab4..a5962b0 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,44 @@ +// Form validators, matching to each of the four errors in order +var validators = [ + // Please enter a valid email address + function () { + return /^.+@.+\..+$/.test($('form input[type=text]').val()); + }, - //insert your code here - -$(function(){ + // Your password should be at least 8 characters long + function () { + return $('form input[type=password]').val().length >= 8; + }, - //insert your code here + // Your password should contain at least one capital letter + function () { + return /[A-Z]/.test($('form input[type=password]').val()); + }, + + // Your password should contain at least one number (0-9) + function () { + return /\d/.test($('form input[type=password]').val()); + } +]; + +// Hides all validation errors +function hideErrors() { + $('.errors li').hide(); +} + +// Runs all validators and display any errors that occur +function validate() { + hideErrors(); + validators.forEach(function (validator, index) { + if (!validator()) { + $('.errors li').eq(index).show(); + } + }); +} + +// Run validators initially and whenever an input is updated +$(function () { + hideErrors(); + validate(); + $('input').on('keyup', validate); }); From d833f392d4deb307e1f73c526643c1634159184a Mon Sep 17 00:00:00 2001 From: Nicolas McCurdy Date: Sat, 10 Jan 2015 23:20:42 -0500 Subject: [PATCH 2/3] Save jQuery elements to variables --- validator.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/validator.js b/validator.js index a5962b0..e5d091f 100644 --- a/validator.js +++ b/validator.js @@ -1,29 +1,34 @@ +// jQuery elements +var $email = $('form input[type=text]'); +var $password = $('form input[type=password]'); +var $errors = $('.errors li'); + // Form validators, matching to each of the four errors in order var validators = [ // Please enter a valid email address function () { - return /^.+@.+\..+$/.test($('form input[type=text]').val()); + return /^.+@.+\..+$/.test($email.val()); }, // Your password should be at least 8 characters long function () { - return $('form input[type=password]').val().length >= 8; + return $password.val().length >= 8; }, // Your password should contain at least one capital letter function () { - return /[A-Z]/.test($('form input[type=password]').val()); + return /[A-Z]/.test($password.val()); }, // Your password should contain at least one number (0-9) function () { - return /\d/.test($('form input[type=password]').val()); + return /\d/.test($password.val()); } ]; // Hides all validation errors function hideErrors() { - $('.errors li').hide(); + $errors.hide(); } // Runs all validators and display any errors that occur @@ -31,7 +36,7 @@ function validate() { hideErrors(); validators.forEach(function (validator, index) { if (!validator()) { - $('.errors li').eq(index).show(); + $errors.eq(index).show(); } }); } @@ -40,5 +45,6 @@ function validate() { $(function () { hideErrors(); validate(); - $('input').on('keyup', validate); + $email.on('keyup', validate); + $password.on('keyup', validate); }); From 611204a2c2c098cb3f5eefed4fb518b4456182a4 Mon Sep 17 00:00:00 2001 From: Nicolas McCurdy Date: Sat, 10 Jan 2015 23:47:37 -0500 Subject: [PATCH 3/3] Disable the submit button when the form is invalid --- validator.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/validator.js b/validator.js index e5d091f..3a539b9 100644 --- a/validator.js +++ b/validator.js @@ -1,6 +1,7 @@ // jQuery elements var $email = $('form input[type=text]'); var $password = $('form input[type=password]'); +var $submit = $('form input[type=submit]'); var $errors = $('.errors li'); // Form validators, matching to each of the four errors in order @@ -29,6 +30,7 @@ var validators = [ // Hides all validation errors function hideErrors() { $errors.hide(); + $submit.prop('disabled', false); } // Runs all validators and display any errors that occur @@ -37,6 +39,7 @@ function validate() { validators.forEach(function (validator, index) { if (!validator()) { $errors.eq(index).show(); + $submit.prop('disabled', true); } }); }