From 3bc6c4a8702d2f9cb400ded2e4fbdb187bfb8d33 Mon Sep 17 00:00:00 2001 From: "G. Wade Johnson" Date: Wed, 7 Oct 2015 16:14:31 -0400 Subject: [PATCH 1/4] Validation with straight jQuery. I went a little overboard with the email validation. But, since I stole the code from a Perl module I released some time ago, I figured it was okay. --- validator.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/validator.js b/validator.js index 860dab4..1f63628 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,58 @@ //insert your code here +function valid_email_local_part(lpart) +{ + if(lpart === undefined || lpart.length > 64 || lpart.length == 0) + return false; + // Localpart is one or more atext pieces separated by .s + return lpart.match(/^[a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+(?:\.[a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+)*$/); +} +function valid_domain_label(label) +{ + if(label === undefined || label.length > 63 || label.length == 0) + return false; + return label.match(/^[a-zA-Z0-9](?:[-a-aA-Z0-9]*[a-zA-Z0-9])?$/); +} +function valid_email_domain(domain) +{ + if(domain === undefined || domain.length > 255 || domain.length == 0) + return false; + // recognize double dots and dot at begin/end because split swallows empty fields + if(domain.match(/^\.|\.\.|\.$/)) + return false; + // a domain is one or more domain labels separated by .s + var labels = domain.split(/\./) + return 0 == $.grep( labels, function(l){ !valid_domain_label(l) } ); +} +function valid_email(email) +{ + var parts = email.split('@'); + if(!valid_email_local_part(parts[0])) + return false; + return valid_email_domain(parts[1]); +} +function display_error_unless(condition, index) +{ + var select = '.errors li:eq(' + index + ')'; + if(condition) + $(select).hide(); + else + $(select).show(); +} $(function(){ //insert your code here + $('.errors li').hide(); + $('input[type="text"]').blur(function(evt){ + var email = evt.target.value; + display_error_unless( valid_email(email), 0 ); + }); + $('input[type="password"]').blur(function(evt){ + // alert('Left password field'); + var 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 ); + }); }); From 905e3ca5450b5235148d6c3a23b402a1872f6301 Mon Sep 17 00:00:00 2001 From: "G. Wade Johnson" Date: Wed, 7 Oct 2015 17:29:04 -0400 Subject: [PATCH 2/4] Stomped on a regex somehow. --- validator.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/validator.js b/validator.js index 1f63628..b893ea9 100644 --- a/validator.js +++ b/validator.js @@ -11,7 +11,7 @@ function valid_domain_label(label) { if(label === undefined || label.length > 63 || label.length == 0) return false; - return label.match(/^[a-zA-Z0-9](?:[-a-aA-Z0-9]*[a-zA-Z0-9])?$/); + return label.match(/^[a-zA-Z0-9](?:[-a-zA-Z0-9]*[a-zA-Z0-9])?$/); } function valid_email_domain(domain) { @@ -42,7 +42,6 @@ function display_error_unless(condition, index) $(function(){ - //insert your code here $('.errors li').hide(); $('input[type="text"]').blur(function(evt){ var email = evt.target.value; From db4ef20e842a530063e619bd3f6c118f2cbe1b1a Mon Sep 17 00:00:00 2001 From: "G. Wade Johnson" Date: Wed, 7 Oct 2015 21:53:38 -0400 Subject: [PATCH 3/4] Fix validator and stop submission on errors. I had an error in the way I used grep that caused validation problems. I fixed that. Then, I added support for stopping submit on errors. --- validator.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/validator.js b/validator.js index b893ea9..c95416c 100644 --- a/validator.js +++ b/validator.js @@ -1,5 +1,3 @@ - - //insert your code here function valid_email_local_part(lpart) { if(lpart === undefined || lpart.length > 64 || lpart.length == 0) @@ -17,12 +15,9 @@ function valid_email_domain(domain) { if(domain === undefined || domain.length > 255 || domain.length == 0) return false; - // recognize double dots and dot at begin/end because split swallows empty fields - if(domain.match(/^\.|\.\.|\.$/)) - return false; // a domain is one or more domain labels separated by .s var labels = domain.split(/\./) - return 0 == $.grep( labels, function(l){ !valid_domain_label(l) } ); + return ($.grep( labels, function(l){ return !valid_domain_label(l) } )).length == 0; } function valid_email(email) { @@ -39,7 +34,7 @@ function display_error_unless(condition, index) else $(select).show(); } - + $(function(){ $('.errors li').hide(); @@ -48,10 +43,16 @@ $(function(){ display_error_unless( valid_email(email), 0 ); }); $('input[type="password"]').blur(function(evt){ - // alert('Left password field'); var 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(function(evt){ + if( $('.errors li').is(':visible') ) { + evt.preventDefault(); + return false; + } + return true; + }); }); From f716f7c150cfc9f98f039431e3914ba1f8390176 Mon Sep 17 00:00:00 2001 From: "G. Wade Johnson" Date: Wed, 7 Oct 2015 22:22:11 -0400 Subject: [PATCH 4/4] Coffeescript version of the javascript validation --- validator.coffee | 50 ++++++++++++++++++++ validator.js | 116 ++++++++++++++++++++++++++--------------------- 2 files changed, 115 insertions(+), 51 deletions(-) create mode 100644 validator.coffee 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 c95416c..3dd17c1 100644 --- a/validator.js +++ b/validator.js @@ -1,58 +1,72 @@ -function valid_email_local_part(lpart) -{ - if(lpart === undefined || lpart.length > 64 || lpart.length == 0) - return false; - // Localpart is one or more atext pieces separated by .s - return lpart.match(/^[a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+(?:\.[a-zA-Z0-9!#\$\%&'*+\-/=?^_`{|}~]+)*$/); -} -function valid_domain_label(label) -{ - if(label === undefined || label.length > 63 || label.length == 0) - return false; +// Generated by CoffeeScript 1.10.0 +(function() { + var display_error_unless, valid_domain_label, valid_email, valid_email_domain, valid_email_local_part; + + 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!#\$\%&'*+\-\/=?^_`{|}~]+)*$/); + }; + + 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])?$/); -} -function valid_email_domain(domain) -{ - if(domain === undefined || domain.length > 255 || domain.length == 0) - return false; - // a domain is one or more domain labels separated by .s - var labels = domain.split(/\./) - return ($.grep( labels, function(l){ return !valid_domain_label(l) } )).length == 0; -} -function valid_email(email) -{ - var parts = email.split('@'); - if(!valid_email_local_part(parts[0])) - return false; + }; + + 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]); -} -function display_error_unless(condition, index) -{ - var select = '.errors li:eq(' + index + ')'; - if(condition) - $(select).hide(); - else - $(select).show(); -} + }; -$(function(){ + display_error_unless = function(condition, index) { + var select; + select = '.errors li:eq(' + index + ')'; + if (condition) { + return $(select).hide(); + } else { + return $(select).show(); + } + }; - $('.errors li').hide(); - $('input[type="text"]').blur(function(evt){ - var email = evt.target.value; - display_error_unless( valid_email(email), 0 ); - }); - $('input[type="password"]').blur(function(evt){ - var 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(function(evt){ - if( $('.errors li').is(':visible') ) { + $(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; + } + return true; + }); }); -}); + +}).call(this);