From 17f6ea0d6f3d06c761e669ab38a1e2224eaea3c2 Mon Sep 17 00:00:00 2001 From: Justin Holm Date: Mon, 15 May 2017 13:25:08 -0400 Subject: [PATCH] Justin Holm solution --- validator.coffee | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ validator.js | 45 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 validator.coffee diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..9f143a0 --- /dev/null +++ b/validator.coffee @@ -0,0 +1,53 @@ +# Hide all initial errors +errors = $('.errors li').hide() + +# Assign variables for Email and Password fields +email = $('input[placeholder=\'Email\']').val() +password = $('input[placeholder=\'Password\']').val() + +# Regex email validator +email_regex = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i + +# Regex password validator types +password_one_num = /(?=^\w{0,}$)(?=\w*[0-9])^\w+$/ +password_one_cap = /(?=^\w{1,}$)(?=\w*[A-Z])^\w+$/ +password_eight_char = /^[a-zA-Z0-9]{8,}$/ + +ValidateForm = -> + email = $('input[placeholder=\'Email\']').val() + password = $('input[placeholder=\'Password\']').val() + ValidateEmail() + ValidatePassword() + return + +ValidateEmail = -> + if email.match(email_regex) + $('.errors li:nth-child(1)').hide() + else + $('.errors li:nth-child(1)').show() + event.preventDefault() + return + +ValidatePassword = -> + if password.length > 8 + $('.errors li:nth-child(2)').hide() + else + $('.errors li:nth-child(2)').show() + event.preventDefault() + if password.match(password_one_cap) + $('.errors li:nth-child(3)').hide() + else + $('.errors li:nth-child(3)').show() + event.preventDefault() + if password.match(password_one_num) + $('.errors li:nth-child(4)').hide() + else + $('.errors li:nth-child(4)').show() + event.preventDefault() + return + +$ -> + $('form').submit -> + ValidateForm() + return + return diff --git a/validator.js b/validator.js index 860dab4..4fe1fa5 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,44 @@ +// Hide all initial errors +var errors = $(".errors li").hide(); - //insert your code here - -$(function(){ +// Assign variables for Email and Password fields +var email = $("input[placeholder='Email']").val(); +var password = $("input[placeholder='Password']").val(); + +// Regex email validator +var email_regex = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i; + +// Regex password validator types +var password_one_num = /(?=^\w{0,}$)(?=\w*[0-9])^\w+$/; +var password_one_cap = /(?=^\w{1,}$)(?=\w*[A-Z])^\w+$/; +var password_eight_char = /^[a-zA-Z0-9]{8,}$/; - //insert your code here +$(function(){ + $("form").submit(function() { + ValidateForm(); + }); }); + +function ValidateForm() { + email = $("input[placeholder='Email']").val(); + password = $("input[placeholder='Password']").val(); + + ValidateEmail(); + ValidatePassword(); +} + +function ValidateEmail() { + if (email.match(email_regex)) $(".errors li:nth-child(1)").hide(); + else $(".errors li:nth-child(1)").show(); event.preventDefault(); +} + +function ValidatePassword() { + if (password.length > 8) $(".errors li:nth-child(2)").hide(); + else $(".errors li:nth-child(2)").show(); event.preventDefault(); + + if (password.match(password_one_cap)) $(".errors li:nth-child(3)").hide(); + else $(".errors li:nth-child(3)").show(); event.preventDefault(); + + if (password.match(password_one_num)) $(".errors li:nth-child(4)").hide(); + else $(".errors li:nth-child(4)").show(); event.preventDefault(); +}