From e6f8eeacd4547d14ab32ec887ae266455cdec20b Mon Sep 17 00:00:00 2001 From: Erik Thiem Date: Fri, 26 May 2017 09:28:29 -0400 Subject: [PATCH 1/2] Added my initial solution --- validator.coffee | 76 +++++++++++++++++++++++++++++ validator.js | 124 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 validator.coffee diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..002b029 --- /dev/null +++ b/validator.coffee @@ -0,0 +1,76 @@ +class Validator + constructor: (@input) -> + + validate: -> + + +class EmailValidator extends Validator + + constructor: (@input) -> + super @input + + validate: -> + ampersandAndPeriodTest = new Test(new RegExp("^([\\w.-]+)@([\\w.-]+)\\.([\\w.-]+)"), @input, "Invalid Email Address. Must have an ampersand and a period.") + + tests = [ampersandAndPeriodTest] + + passing = true + @errorMessages = [] + + for test in tests + if not test.passes() + passing = false + @errorMessages.push test.errorMessage + + return passing + + +class PasswordValidator extends Validator + + validate: -> + lengthTest = new Test(new RegExp("^(.+){8,}"), @input, "Password must be at least 8 characters long.") + capitalTest = new Test(new RegExp(".*[A-Z]+.*"), @input, "Password must have at least one capital letter.") + numberTest = new Test(new RegExp(".*[0-9]+.*"), @input, "Password must have at least one number.") + + tests = [lengthTest, capitalTest, numberTest] + + passing = true + @errorMessages = [] + + for test in tests + if not test.passes() + passing = false + @errorMessages.push test.errorMessage + + return passing + +class Test + + constructor: (@regex, @input, @errorMessage) -> + + passes: -> + @regex.test @input + +errorMessageDomElement = $("ul.errors") +errorMessageDomElement.empty() + +form = $('form[name="sign_in"]') + +form.submit -> + + errorMessageDomElement.empty() + + email = form.find("input[type=text]").val() + password = form.find("input[type=password]").val() + + emailValidator = new EmailValidator email + passwordValidator = new PasswordValidator password + + validators = [emailValidator, passwordValidator] + + validationResults = validators.map (validator) -> validator.validate() + errorMessages = (validators.map (validator) -> validator.errorMessages).reduce (a, b) -> a.concat b + + for message in errorMessages + liElement = "
  • " + message + "
  • " + errorMessageDomElement.append liElement diff --git a/validator.js b/validator.js index 860dab4..1bca3f8 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,121 @@ +// Generated by CoffeeScript 1.12.6 +(function() { + var EmailValidator, PasswordValidator, Test, Validator, errorMessageDomElement, form, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; - //insert your code here - -$(function(){ + Validator = (function() { + function Validator(input) { + this.input = input; + } - //insert your code here -}); + Validator.prototype.validate = function() {}; + + return Validator; + + })(); + + EmailValidator = (function(superClass) { + extend(EmailValidator, superClass); + + function EmailValidator(input) { + this.input = input; + EmailValidator.__super__.constructor.call(this, this.input); + } + + EmailValidator.prototype.validate = function() { + var ampersandAndPeriodTest, i, len, passing, test, tests; + ampersandAndPeriodTest = new Test(new RegExp("^([\\w.-]+)@([\\w.-]+)\\.([\\w.-]+)"), this.input, "Invalid Email Address. Must have an ampersand and a period."); + tests = [ampersandAndPeriodTest]; + passing = true; + this.errorMessages = []; + for (i = 0, len = tests.length; i < len; i++) { + test = tests[i]; + if (!test.passes()) { + passing = false; + this.errorMessages.push(test.errorMessage); + } + } + return passing; + }; + + return EmailValidator; + + })(Validator); + + PasswordValidator = (function(superClass) { + extend(PasswordValidator, superClass); + + function PasswordValidator() { + return PasswordValidator.__super__.constructor.apply(this, arguments); + } + + PasswordValidator.prototype.validate = function() { + var capitalTest, i, len, lengthTest, numberTest, passing, test, tests; + lengthTest = new Test(new RegExp("^(.+){8,}"), this.input, "Password must be at least 8 characters long."); + capitalTest = new Test(new RegExp(".*[A-Z]+.*"), this.input, "Password must have at least one capital letter."); + numberTest = new Test(new RegExp(".*[0-9]+.*"), this.input, "Password must have at least one number."); + tests = [lengthTest, capitalTest, numberTest]; + passing = true; + this.errorMessages = []; + for (i = 0, len = tests.length; i < len; i++) { + test = tests[i]; + if (!test.passes()) { + passing = false; + this.errorMessages.push(test.errorMessage); + } + } + return passing; + }; + + return PasswordValidator; + + })(Validator); + + Test = (function() { + function Test(regex, input, errorMessage) { + this.regex = regex; + this.input = input; + this.errorMessage = errorMessage; + } + + Test.prototype.passes = function() { + return this.regex.test(this.input); + }; + + return Test; + + })(); + + errorMessageDomElement = $("ul.errors"); + + errorMessageDomElement.empty(); + + form = $('form[name="sign_in"]'); + + form.submit(function() { + var email, emailValidator, errorMessages, i, len, liElement, message, password, passwordValidator, results, validationResults, validators; + errorMessageDomElement.empty(); + email = form.find("input[type=text]").val(); + password = form.find("input[type=password]").val(); + emailValidator = new EmailValidator(email); + passwordValidator = new PasswordValidator(password); + validators = [emailValidator, passwordValidator]; + validationResults = validators.map(function(validator) { + return validator.validate(); + }); + errorMessages = (validators.map(function(validator) { + return validator.errorMessages; + })).reduce(function(a, b) { + return a.concat(b); + }); + results = []; + for (i = 0, len = errorMessages.length; i < len; i++) { + message = errorMessages[i]; + liElement = "
  • " + message + "
  • "; + results.push(errorMessageDomElement.append(liElement)); + } + return results; + }); + +}).call(this); From a1e6c47ce04c88ef7a23784584c0dc559bef752a Mon Sep 17 00:00:00 2001 From: Erik Thiem Date: Fri, 26 May 2017 09:40:14 -0400 Subject: [PATCH 2/2] Refactored --- validator.coffee | 37 ++++++++++++++------------------- validator.js | 53 ++++++++++++++++++++++-------------------------- 2 files changed, 39 insertions(+), 51 deletions(-) diff --git a/validator.coffee b/validator.coffee index 002b029..9eac213 100644 --- a/validator.coffee +++ b/validator.coffee @@ -1,23 +1,12 @@ class Validator - constructor: (@input) -> + constructor: (@input, @tests) -> validate: -> - -class EmailValidator extends Validator - - constructor: (@input) -> - super @input - - validate: -> - ampersandAndPeriodTest = new Test(new RegExp("^([\\w.-]+)@([\\w.-]+)\\.([\\w.-]+)"), @input, "Invalid Email Address. Must have an ampersand and a period.") - - tests = [ampersandAndPeriodTest] - passing = true @errorMessages = [] - for test in tests + for test in @tests if not test.passes() passing = false @errorMessages.push test.errorMessage @@ -25,6 +14,16 @@ class EmailValidator extends Validator return passing +class EmailValidator extends Validator + + validate: -> + ampersandAndPeriodTest = new Test(new RegExp("^([\\w.-]+)@([\\w.-]+)\\.([\\w.-]+)"), @input, "Invalid Email Address. Must have an ampersand and a period.") + + @tests = [ampersandAndPeriodTest] + + super + + class PasswordValidator extends Validator validate: -> @@ -32,17 +31,10 @@ class PasswordValidator extends Validator capitalTest = new Test(new RegExp(".*[A-Z]+.*"), @input, "Password must have at least one capital letter.") numberTest = new Test(new RegExp(".*[0-9]+.*"), @input, "Password must have at least one number.") - tests = [lengthTest, capitalTest, numberTest] - - passing = true - @errorMessages = [] + @tests = [lengthTest, capitalTest, numberTest] - for test in tests - if not test.passes() - passing = false - @errorMessages.push test.errorMessage + super - return passing class Test @@ -51,6 +43,7 @@ class Test passes: -> @regex.test @input + errorMessageDomElement = $("ul.errors") errorMessageDomElement.empty() diff --git a/validator.js b/validator.js index 1bca3f8..8195238 100644 --- a/validator.js +++ b/validator.js @@ -5,11 +5,25 @@ hasProp = {}.hasOwnProperty; Validator = (function() { - function Validator(input) { + function Validator(input, tests) { this.input = input; + this.tests = tests; } - Validator.prototype.validate = function() {}; + Validator.prototype.validate = function() { + var i, len, passing, ref, test; + passing = true; + this.errorMessages = []; + ref = this.tests; + for (i = 0, len = ref.length; i < len; i++) { + test = ref[i]; + if (!test.passes()) { + passing = false; + this.errorMessages.push(test.errorMessage); + } + } + return passing; + }; return Validator; @@ -18,25 +32,15 @@ EmailValidator = (function(superClass) { extend(EmailValidator, superClass); - function EmailValidator(input) { - this.input = input; - EmailValidator.__super__.constructor.call(this, this.input); + function EmailValidator() { + return EmailValidator.__super__.constructor.apply(this, arguments); } EmailValidator.prototype.validate = function() { - var ampersandAndPeriodTest, i, len, passing, test, tests; + var ampersandAndPeriodTest; ampersandAndPeriodTest = new Test(new RegExp("^([\\w.-]+)@([\\w.-]+)\\.([\\w.-]+)"), this.input, "Invalid Email Address. Must have an ampersand and a period."); - tests = [ampersandAndPeriodTest]; - passing = true; - this.errorMessages = []; - for (i = 0, len = tests.length; i < len; i++) { - test = tests[i]; - if (!test.passes()) { - passing = false; - this.errorMessages.push(test.errorMessage); - } - } - return passing; + this.tests = [ampersandAndPeriodTest]; + return EmailValidator.__super__.validate.apply(this, arguments); }; return EmailValidator; @@ -51,21 +55,12 @@ } PasswordValidator.prototype.validate = function() { - var capitalTest, i, len, lengthTest, numberTest, passing, test, tests; + var capitalTest, lengthTest, numberTest; lengthTest = new Test(new RegExp("^(.+){8,}"), this.input, "Password must be at least 8 characters long."); capitalTest = new Test(new RegExp(".*[A-Z]+.*"), this.input, "Password must have at least one capital letter."); numberTest = new Test(new RegExp(".*[0-9]+.*"), this.input, "Password must have at least one number."); - tests = [lengthTest, capitalTest, numberTest]; - passing = true; - this.errorMessages = []; - for (i = 0, len = tests.length; i < len; i++) { - test = tests[i]; - if (!test.passes()) { - passing = false; - this.errorMessages.push(test.errorMessage); - } - } - return passing; + this.tests = [lengthTest, capitalTest, numberTest]; + return PasswordValidator.__super__.validate.apply(this, arguments); }; return PasswordValidator;