diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..8c199d8 --- /dev/null +++ b/validator.coffee @@ -0,0 +1,50 @@ +valid_email_local_part = (lpart) -> + if lpart is undefined || lpart.length == 0 || lpart.length >64 + return false + # Localpart is one or more atext pieces separated by .s + return lpart.match(/^[a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+(?:\.[a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+)*$/) + +valid_domain_label = (label) -> + if label is undefined || label.length > 63 || label.length == 0 + return false + return label.match(/^[a-zA-Z0-9](?:[-a-zA-Z0-9]*[a-zA-Z0-9])?$/) + +valid_email_domain = (domain) -> + if domain is undefined || domain.length > 255 || domain.length == 0 + return false + # a domain is one or more domain labels separated by .s + labels = domain.split(/\./) + return ($.grep( labels, (l)->return !valid_domain_label(l) )).length == 0 + +valid_email = (email) -> + parts = email.split('@') + return false if !valid_email_local_part(parts[0]) + return valid_email_domain(parts[1]) + +display_error_unless = (condition, index) -> + select = '.errors li:eq(' + index + ')' + if(condition) + $(select).hide() + else + $(select).show() + + +$(() -> + $('.errors li').hide() + $('input[type="text"]').blur((evt) -> + email = evt.target.value + display_error_unless( valid_email(email), 0 ) + ) + $('input[type="password"]').blur((evt) -> + password = evt.target.value + display_error_unless( password.length >= 8, 1 ) + display_error_unless( password.match(/[A-Z]/), 2 ) + display_error_unless( password.match(/\d/), 3 ) + ) + $('form[name="sign_in"]').submit((evt) -> + if $('.errors li').is(':visible') + evt.preventDefault() + return false + return true + ) +) diff --git a/validator.js b/validator.js index 860dab4..3dd17c1 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,72 @@ +// Generated by CoffeeScript 1.10.0 +(function() { + var display_error_unless, valid_domain_label, valid_email, valid_email_domain, valid_email_local_part; - //insert your code here - -$(function(){ + valid_email_local_part = function(lpart) { + if (lpart === void 0 || lpart.length === 0 || lpart.length > 64) { + return false; + } + return lpart.match(/^[a-zA-Z0-9!#\$\%&'*+\-\/=?^_`{|}~]+(?:\.[a-zA-Z0-9!#\$\%&'*+\-\/=?^_`{|}~]+)*$/); + }; - //insert your code here -}); + valid_domain_label = function(label) { + if (label === void 0 || label.length > 63 || label.length === 0) { + return false; + } + return label.match(/^[a-zA-Z0-9](?:[-a-zA-Z0-9]*[a-zA-Z0-9])?$/); + }; + + valid_email_domain = function(domain) { + var labels; + if (domain === void 0 || domain.length > 255 || domain.length === 0) { + return false; + } + labels = domain.split(/\./); + return ($.grep(labels, function(l) { + return !valid_domain_label(l); + })).length === 0; + }; + + valid_email = function(email) { + var parts; + parts = email.split('@'); + if (!valid_email_local_part(parts[0])) { + return false; + } + return valid_email_domain(parts[1]); + }; + + display_error_unless = function(condition, index) { + var select; + select = '.errors li:eq(' + index + ')'; + if (condition) { + return $(select).hide(); + } else { + return $(select).show(); + } + }; + + $(function() { + $('.errors li').hide(); + $('input[type="text"]').blur(function(evt) { + var email; + email = evt.target.value; + return display_error_unless(valid_email(email), 0); + }); + $('input[type="password"]').blur(function(evt) { + var password; + password = evt.target.value; + display_error_unless(password.length >= 8, 1); + display_error_unless(password.match(/[A-Z]/), 2); + return display_error_unless(password.match(/\d/), 3); + }); + return $('form[name="sign_in"]').submit(function(evt) { + if ($('.errors li').is(':visible')) { + evt.preventDefault(); + return false; + } + return true; + }); + }); + +}).call(this);