From f18e6099ae5b4d0b5ba4db15df2d2f908c34b270 Mon Sep 17 00:00:00 2001 From: Robert Lude Date: Mon, 26 Jan 2015 10:46:20 -0500 Subject: [PATCH] solved --- validator.coffee | 75 ++++++++++++++++++++++++++++++ validator.js | 118 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 188 insertions(+), 5 deletions(-) create mode 100644 validator.coffee diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..86dd2c3 --- /dev/null +++ b/validator.coffee @@ -0,0 +1,75 @@ + +# constants + +EMAIL_REGEX = /// # this is NOT comprehensive and only allows common format emails + [a-z0-9._-]+ + @ + [a-z0-9._-]+ + \. + [a-z0-9._-]+ +///i + +ERROR_MESSAGE_MAP = # edit this if error message types or their ordering changes + invalidEmail: 0 + passwordTooShort: 1 + passwordMissingDigit: 2 + passwordMissingCapitalLetter: 3 + +# html elements we will use + +loginForm = $ "form" +emailInput = loginForm.children("input[type='text']") +passwordInput = loginForm.children("input[type='password']") +submitButton = loginForm.children("input[type='submit']") +errorMessages = $ ".errors li" + +# helper functions + +getEmailErrors = (typing) -> + emailAddress = emailInput.val() + return {} if emailAddress.length == 0 && typing + invalidEmail: not EMAIL_REGEX.test emailAddress + +getPasswordErrors = (typing) -> + password = passwordInput.val() + return {} if password.length == 0 && typing + passwordTooShort: password.length < 8 + passwordMissingDigit: not /\d/.test password + passwordMissingCapitalLetter: not /[A-Z]/.test password + +getAllErrors = (typing) -> + $.extend {}, getEmailErrors(typing), getPasswordErrors(typing) + +isErrorFree = (errors) -> + for own errorKey, hasError of errors + return false if hasError + true + +updateErrorVisibility = (errors) -> + for own errorKey, errorIndex of ERROR_MESSAGE_MAP + errorMessages.eq(errorIndex).toggle !!errors[errorKey] + +doTypingErrorCheck = () -> + errors = getAllErrors true + updateErrorVisibility errors + if (errorKey for own errorKey of errors).length > 0 && isErrorFree errors + submitButton.removeAttr "disabled" + else + submitButton.attr "disabled", "disabled" + +# dom events + +loginForm.submit (event) -> + errors = getAllErrors(false) + return if isErrorFree(errors) + updateErrorVisibility errors + event.preventDefault() + +emailInput.change doTypingErrorCheck +emailInput.keyup doTypingErrorCheck +passwordInput.change doTypingErrorCheck +passwordInput.keyup doTypingErrorCheck + +# initialization + +doTypingErrorCheck() diff --git a/validator.js b/validator.js index 860dab4..100c54e 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,115 @@ +// Generated by CoffeeScript 1.8.0 +(function() { + var EMAIL_REGEX, ERROR_MESSAGE_MAP, doTypingErrorCheck, emailInput, errorMessages, getAllErrors, getEmailErrors, getPasswordErrors, isErrorFree, loginForm, passwordInput, submitButton, updateErrorVisibility, + __hasProp = {}.hasOwnProperty; - //insert your code here - -$(function(){ + EMAIL_REGEX = /[a-z0-9._-]+@[a-z0-9._-]+\.[a-z0-9._-]+/i; - //insert your code here -}); + ERROR_MESSAGE_MAP = { + invalidEmail: 0, + passwordTooShort: 1, + passwordMissingDigit: 2, + passwordMissingCapitalLetter: 3 + }; + + loginForm = $("form"); + + emailInput = loginForm.children("input[type='text']"); + + passwordInput = loginForm.children("input[type='password']"); + + submitButton = loginForm.children("input[type='submit']"); + + errorMessages = $(".errors li"); + + getEmailErrors = function(typing) { + var emailAddress; + emailAddress = emailInput.val(); + if (emailAddress.length === 0 && typing) { + return {}; + } + return { + invalidEmail: !EMAIL_REGEX.test(emailAddress) + }; + }; + + getPasswordErrors = function(typing) { + var password; + password = passwordInput.val(); + if (password.length === 0 && typing) { + return {}; + } + return { + passwordTooShort: password.length < 8, + passwordMissingDigit: !/\d/.test(password), + passwordMissingCapitalLetter: !/[A-Z]/.test(password) + }; + }; + + getAllErrors = function(typing) { + return $.extend({}, getEmailErrors(typing), getPasswordErrors(typing)); + }; + + isErrorFree = function(errors) { + var errorKey, hasError; + for (errorKey in errors) { + if (!__hasProp.call(errors, errorKey)) continue; + hasError = errors[errorKey]; + if (hasError) { + return false; + } + } + return true; + }; + + updateErrorVisibility = function(errors) { + var errorIndex, errorKey, _results; + _results = []; + for (errorKey in ERROR_MESSAGE_MAP) { + if (!__hasProp.call(ERROR_MESSAGE_MAP, errorKey)) continue; + errorIndex = ERROR_MESSAGE_MAP[errorKey]; + _results.push(errorMessages.eq(errorIndex).toggle(!!errors[errorKey])); + } + return _results; + }; + + doTypingErrorCheck = function() { + var errorKey, errors; + errors = getAllErrors(true); + updateErrorVisibility(errors); + if (((function() { + var _results; + _results = []; + for (errorKey in errors) { + if (!__hasProp.call(errors, errorKey)) continue; + _results.push(errorKey); + } + return _results; + })()).length > 0 && isErrorFree(errors)) { + return submitButton.removeAttr("disabled"); + } else { + return submitButton.attr("disabled", "disabled"); + } + }; + + loginForm.submit(function(event) { + var errors; + errors = getAllErrors(false); + if (isErrorFree(errors)) { + return; + } + updateErrorVisibility(errors); + return event.preventDefault(); + }); + + emailInput.change(doTypingErrorCheck); + + emailInput.keyup(doTypingErrorCheck); + + passwordInput.change(doTypingErrorCheck); + + passwordInput.keyup(doTypingErrorCheck); + + doTypingErrorCheck(); + +}).call(this);