diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..3d344fe --- /dev/null +++ b/validator.coffee @@ -0,0 +1,40 @@ +class Test + constructor: (@sel, @func, @errMsg) -> + + run: -> if @func $(@sel).val() then null else @errMsg + +class Validator + constructor: (@errList) -> + @tests = [] + + addTest: (t) -> + @tests.push t + + check: -> + $(@errList).empty() + valid = true + for t in @tests + error = t.run() + if error != null + $(@errList).append '
  • ' + error + '
  • ' + valid = false + valid + +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 diff --git a/validator.js b/validator.js index 860dab4..bdb5de0 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,90 @@ +// Generated by CoffeeScript 1.12.6 +(function() { + var Test, Validator, digitRegex, emailRegex, upperCaseRegex, v; - //insert your code here - -$(function(){ + Test = (function() { + function Test(sel, func, errMsg) { + this.sel = sel; + this.func = func; + this.errMsg = errMsg; + } - //insert your code here -}); + Test.prototype.run = function() { + if (this.func($(this.sel).val())) { + return null; + } else { + return this.errMsg; + } + }; + + 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; + 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; + }; + + return Validator; + + })(); + + 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')); + + v.addTest(new Test('input[type=password]', (function(s) { + return s.length >= 8; + }), 'Your password should be at least 8 characters long')); + + v.addTest(new Test('input[type=password]', (function(s) { + return upperCaseRegex.test(s); + }), 'Your password should contain at least one capital letter')); + + v.addTest(new Test('input[type=password]', (function(s) { + return digitRegex.test(s); + }), 'Your password should contain at least one number (0-9)')); + + $('ul.errors').empty(); + + $('input[type=submit]').click(function(e) { + if (!v.check()) { + e.preventDefault(); + } else { + $('form').submit(); + alert('Sign in successful!'); + } + return null; + }); + +}).call(this);