From 60e5502cb7b38f393fbc76f18f44544cfe99bc07 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 24 May 2017 12:45:56 -0400 Subject: [PATCH 1/2] Added solution --- validator.coffee | 45 ++++++++++++++++++++++++++++ validator.js | 77 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 validator.coffee diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..9ba9191 --- /dev/null +++ b/validator.coffee @@ -0,0 +1,45 @@ +class Validator + constructor: (@emailSel, @passSel, @errSel) -> + @emailRegex = /^([a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+(?:\.*[a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+)*)@([a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+)$/ + @upperCaseRegex = /.*[A-Z]+.*/ + @digitRegex = /.*[0-9]+.*/ + + check: -> + $(@errSel).empty() + @checkEmail() && @checkPassword() + + checkEmail: -> + s = $(@emailSel).val() + if not @emailRegex.test(s) + $(@errSel).append '
  • Please enter a valid email address
  • ' + false + else + true + + checkPassword: -> + s = $(@passSel).val() + valid = true + if s.length < 8 + $(@errSel).append '
  • Your password should be at least 8 characters long
  • ' + valid = false + if not @upperCaseRegex.test s + $(@errSel).append '
  • Your password should contain at least one capital letter
  • ' + valid = false + if not @digitRegex.test(s) + $(@errSel).append '
  • Your password should contain at least one number (0-9)
  • ' + valid = false + valid + +email = 'input[type=text]' +pass = 'input[type=password]' +err = 'ul.errors' +$(err).empty() +v = new Validator email, pass, err + +$('input[type=submit]').click (e) -> + if not v.check() + e.preventDefault() + else + $('form').submit() + alert 'Sign in successful!' + null \ No newline at end of file diff --git a/validator.js b/validator.js index 860dab4..f7bf92d 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,74 @@ +// Generated by CoffeeScript 1.12.6 +(function() { + var Validator, email, err, pass, v; - //insert your code here - -$(function(){ + Validator = (function() { + function Validator(emailSel, passSel, errSel) { + this.emailSel = emailSel; + this.passSel = passSel; + this.errSel = errSel; + this.emailRegex = /^([a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+(?:\.*[a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+)*)@([a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+)$/; + this.upperCaseRegex = /.*[A-Z]+.*/; + this.digitRegex = /.*[0-9]+.*/; + } - //insert your code here -}); + Validator.prototype.check = function() { + $(this.errSel).empty(); + return this.checkEmail() && this.checkPassword(); + }; + + Validator.prototype.checkEmail = function() { + var s; + s = $(this.emailSel).val(); + if (!this.emailRegex.test(s)) { + $(this.errSel).append('
  • Please enter a valid email address
  • '); + return false; + } else { + return true; + } + }; + + Validator.prototype.checkPassword = function() { + var s, valid; + s = $(this.passSel).val(); + valid = true; + if (s.length < 8) { + $(this.errSel).append('
  • Your password should be at least 8 characters long
  • '); + valid = false; + } + if (!this.upperCaseRegex.test(s)) { + $(this.errSel).append('
  • Your password should contain at least one capital letter
  • '); + valid = false; + } + if (!this.digitRegex.test(s)) { + $(this.errSel).append('
  • Your password should contain at least one number (0-9)
  • '); + valid = false; + } + return valid; + }; + + return Validator; + + })(); + + email = 'input[type=text]'; + + pass = 'input[type=password]'; + + err = 'ul.errors'; + + $(err).empty(); + + v = new Validator(email, pass, err); + + $('input[type=submit]').click(function(e) { + if (!v.check()) { + e.preventDefault(); + } else { + $('form').submit(); + alert('Sign in successful!'); + } + return null; + }); + +}).call(this); From afa3c1607d307b811f18b1b5ebe677a2af9d116a Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 26 May 2017 09:17:57 -0400 Subject: [PATCH 2/2] Updated script to make it more extensible --- validator.coffee | 59 ++++++++++++++--------------- validator.js | 96 ++++++++++++++++++++++++++++-------------------- 2 files changed, 83 insertions(+), 72 deletions(-) diff --git a/validator.coffee b/validator.coffee index 9ba9191..3d344fe 100644 --- a/validator.coffee +++ b/validator.coffee @@ -1,45 +1,40 @@ -class Validator - constructor: (@emailSel, @passSel, @errSel) -> - @emailRegex = /^([a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+(?:\.*[a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+)*)@([a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+)$/ - @upperCaseRegex = /.*[A-Z]+.*/ - @digitRegex = /.*[0-9]+.*/ +class Test + constructor: (@sel, @func, @errMsg) -> - check: -> - $(@errSel).empty() - @checkEmail() && @checkPassword() + run: -> if @func $(@sel).val() then null else @errMsg + +class Validator + constructor: (@errList) -> + @tests = [] - checkEmail: -> - s = $(@emailSel).val() - if not @emailRegex.test(s) - $(@errSel).append '
  • Please enter a valid email address
  • ' - false - else - true + addTest: (t) -> + @tests.push t - checkPassword: -> - s = $(@passSel).val() + check: -> + $(@errList).empty() valid = true - if s.length < 8 - $(@errSel).append '
  • Your password should be at least 8 characters long
  • ' - valid = false - if not @upperCaseRegex.test s - $(@errSel).append '
  • Your password should contain at least one capital letter
  • ' - valid = false - if not @digitRegex.test(s) - $(@errSel).append '
  • Your password should contain at least one number (0-9)
  • ' - valid = false + for t in @tests + error = t.run() + if error != null + $(@errList).append '
  • ' + error + '
  • ' + valid = false valid -email = 'input[type=text]' -pass = 'input[type=password]' -err = 'ul.errors' -$(err).empty() -v = new Validator email, pass, err +emailRegex = /^([a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+(?:\.*[a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+)*)@([a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+)$/ +upperCaseRegex = /.*[A-Z]+.*/ +digitRegex = /.*[0-9]+.*/ + +v = new Validator 'ul.errors' +v.addTest new Test 'input[type=text]', ((s) -> emailRegex.test s), 'Please enter a valid email address' +v.addTest new Test 'input[type=password]', ((s) -> s.length >= 8), 'Your password should be at least 8 characters long' +v.addTest new Test 'input[type=password]', ((s) -> upperCaseRegex.test s), 'Your password should contain at least one capital letter' +v.addTest new Test 'input[type=password]', ((s) -> digitRegex.test s), 'Your password should contain at least one number (0-9)' +$('ul.errors').empty() $('input[type=submit]').click (e) -> if not v.check() e.preventDefault() else $('form').submit() alert 'Sign in successful!' - null \ No newline at end of file + null diff --git a/validator.js b/validator.js index f7bf92d..bdb5de0 100644 --- a/validator.js +++ b/validator.js @@ -1,48 +1,48 @@ // Generated by CoffeeScript 1.12.6 (function() { - var Validator, email, err, pass, v; + var Test, Validator, digitRegex, emailRegex, upperCaseRegex, v; - Validator = (function() { - function Validator(emailSel, passSel, errSel) { - this.emailSel = emailSel; - this.passSel = passSel; - this.errSel = errSel; - this.emailRegex = /^([a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+(?:\.*[a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+)*)@([a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+)$/; - this.upperCaseRegex = /.*[A-Z]+.*/; - this.digitRegex = /.*[0-9]+.*/; + Test = (function() { + function Test(sel, func, errMsg) { + this.sel = sel; + this.func = func; + this.errMsg = errMsg; } - Validator.prototype.check = function() { - $(this.errSel).empty(); - return this.checkEmail() && this.checkPassword(); - }; - - Validator.prototype.checkEmail = function() { - var s; - s = $(this.emailSel).val(); - if (!this.emailRegex.test(s)) { - $(this.errSel).append('
  • Please enter a valid email address
  • '); - return false; + Test.prototype.run = function() { + if (this.func($(this.sel).val())) { + return null; } else { - return true; + return this.errMsg; } }; - Validator.prototype.checkPassword = function() { - var s, valid; - s = $(this.passSel).val(); + return Test; + + })(); + + Validator = (function() { + function Validator(errList) { + this.errList = errList; + this.tests = []; + } + + Validator.prototype.addTest = function(t) { + return this.tests.push(t); + }; + + Validator.prototype.check = function() { + var error, i, len, ref, t, valid; + $(this.errList).empty(); valid = true; - if (s.length < 8) { - $(this.errSel).append('
  • Your password should be at least 8 characters long
  • '); - valid = false; - } - if (!this.upperCaseRegex.test(s)) { - $(this.errSel).append('
  • Your password should contain at least one capital letter
  • '); - valid = false; - } - if (!this.digitRegex.test(s)) { - $(this.errSel).append('
  • Your password should contain at least one number (0-9)
  • '); - valid = false; + ref = this.tests; + for (i = 0, len = ref.length; i < len; i++) { + t = ref[i]; + error = t.run(); + if (error !== null) { + $(this.errList).append('
  • ' + error + '
  • '); + valid = false; + } } return valid; }; @@ -51,15 +51,31 @@ })(); - email = 'input[type=text]'; + emailRegex = /^([a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+(?:\.*[a-zA-Z0-9!#$%&'*\+\-\/=?^_`{|}~]+)*)@([a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)+)$/; + + upperCaseRegex = /.*[A-Z]+.*/; + + digitRegex = /.*[0-9]+.*/; + + v = new Validator('ul.errors'); + + v.addTest(new Test('input[type=text]', (function(s) { + return emailRegex.test(s); + }), 'Please enter a valid email address')); - pass = 'input[type=password]'; + v.addTest(new Test('input[type=password]', (function(s) { + return s.length >= 8; + }), 'Your password should be at least 8 characters long')); - err = 'ul.errors'; + v.addTest(new Test('input[type=password]', (function(s) { + return upperCaseRegex.test(s); + }), 'Your password should contain at least one capital letter')); - $(err).empty(); + v.addTest(new Test('input[type=password]', (function(s) { + return digitRegex.test(s); + }), 'Your password should contain at least one number (0-9)')); - v = new Validator(email, pass, err); + $('ul.errors').empty(); $('input[type=submit]').click(function(e) { if (!v.check()) {