diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..9eac213 --- /dev/null +++ b/validator.coffee @@ -0,0 +1,69 @@ +class Validator + constructor: (@input, @tests) -> + + validate: -> + + passing = true + @errorMessages = [] + + for test in @tests + if not test.passes() + passing = false + @errorMessages.push test.errorMessage + + 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: -> + 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] + + super + + +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..8195238 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,116 @@ +// 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, tests) { + this.input = input; + this.tests = tests; + } - //insert your code here -}); + 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; + + })(); + + EmailValidator = (function(superClass) { + extend(EmailValidator, superClass); + + function EmailValidator() { + return EmailValidator.__super__.constructor.apply(this, arguments); + } + + EmailValidator.prototype.validate = function() { + var ampersandAndPeriodTest; + ampersandAndPeriodTest = new Test(new RegExp("^([\\w.-]+)@([\\w.-]+)\\.([\\w.-]+)"), this.input, "Invalid Email Address. Must have an ampersand and a period."); + this.tests = [ampersandAndPeriodTest]; + return EmailValidator.__super__.validate.apply(this, arguments); + }; + + return EmailValidator; + + })(Validator); + + PasswordValidator = (function(superClass) { + extend(PasswordValidator, superClass); + + function PasswordValidator() { + return PasswordValidator.__super__.constructor.apply(this, arguments); + } + + PasswordValidator.prototype.validate = function() { + 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."); + this.tests = [lengthTest, capitalTest, numberTest]; + return PasswordValidator.__super__.validate.apply(this, arguments); + }; + + 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);