From e4dc21b4659171c60803518964532ecabeefe395 Mon Sep 17 00:00:00 2001 From: Jonah Roth Date: Tue, 31 May 2016 11:23:10 -0400 Subject: [PATCH 1/2] Correct and extendible form in coffeescript --- validator.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/validator.js b/validator.js index 860dab4..30aaa33 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,68 @@ +// Generated by CoffeeScript 1.10.0 +(function() { + var email_field, error_display, error_hide, error_list, form, pwd_field, submit_button; - //insert your code here - -$(function(){ + error_display = function(el) { + return el.css('display', 'auto'); + }; - //insert your code here -}); + error_hide = function(el) { + return el.css('display', 'none'); + }; + + form = $('form[name="sign_in"]'); + + email_field = form.children().eq(0); + + pwd_field = form.children().eq(2); + + submit_button = $('input[type="submit"]'); + + error_list = $('.errors').children(); + + error_hide(error_list); + + $(function() { + form.submit(function() { + var conditions, email, email_regex, i, j, len, len1, pwd, x; + email = email_field.val(); + pwd = pwd_field.val(); + email_regex = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i; + conditions = [ + { + condition: (email.match(email_regex)) !== null, + error_no: 0 + }, { + condition: pwd.length >= 8, + error_no: 1 + }, { + condition: (pwd.match(/[A-Z]/)) !== null, + error_no: 2 + }, { + condition: (pwd.match(/\d/)) !== null, + error_no: 3 + } + ]; + console.log(conditions); + for (i = 0, len = conditions.length; i < len; i++) { + x = conditions[i]; + error_hide(error_list.eq(x.error_no)); + } + for (j = 0, len1 = conditions.length; j < len1; j++) { + x = conditions[j]; + if (x.condition) { + error_hide(error_list.eq(x.error_no)); + } else { + error_display(error_list.eq(x.error_no)); + } + } + if (conditions.every(function(el) { + return el.condition === true; + })) { + return alert("Success"); + } + }); + return true; + }); + +}).call(this); From 05f6672e5336c1fddf0493ec754ba7f8133d85e6 Mon Sep 17 00:00:00 2001 From: Jonah Roth Date: Tue, 31 May 2016 11:26:26 -0400 Subject: [PATCH 2/2] Included coffeescript file this time --- validator.coffee | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 validator.coffee diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..854426d --- /dev/null +++ b/validator.coffee @@ -0,0 +1,54 @@ +error_display = (el) -> el.css('display', 'auto') +error_hide = (el) -> el.css('display', 'none') + +form = $('form[name="sign_in"]') +email_field = form.children().eq(0) +pwd_field = form.children().eq(2) +submit_button = $('input[type="submit"]') + +error_list = $('.errors').children() + +error_hide error_list + + +$( -> + form.submit( -> + + email = email_field.val() + pwd = pwd_field.val() + email_regex = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i + conditions = [ + { + condition: (email.match email_regex) != null + error_no: 0 + }, { + condition: pwd.length >= 8 + error_no: 1 + }, { + condition: (pwd.match /[A-Z]/) != null + error_no: 2 + }, { + condition: (pwd.match /\d/) != null + error_no: 3 + } + ] + + console.log conditions + + (error_hide error_list.eq(x.error_no)) for x in conditions + ( + if x.condition + error_hide error_list.eq(x.error_no) + else + error_display error_list.eq(x.error_no) + ) for x in conditions + + if conditions.every((el) -> el.condition == true) + alert "Success" + + ) + true + + + +)