diff --git a/index.html b/index.html index 7cf849d..e942ce7 100644 --- a/index.html +++ b/index.html @@ -29,5 +29,6 @@

Sign In

+ diff --git a/validator.js b/validator.js index 860dab4..0db8953 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,43 @@ +$(document).ready(function() { + $(".errors").children().hide(); - //insert your code here - -$(function(){ + $("form").submit(function() { + validateEmail(); + validatePassword(); + }); - //insert your code here }); + +var form = document.forms["sign_in"]; + +function validateEmail() { + email = /(^[a-z0-9_])+(.|[_])*([a-z0-9])*@([a-z]+).+/; + var validateEmail = form.elements[0].value; + if (validateEmail == null || validateEmail == "") { + $(".errors li:nth-child(1)").show(); + } + if (!email) { + $(".errors li:nth-child(1)").show(); + } +} + +function validatePassword() { + var password = form.elements[1].value; + var hasDigit = /[0-9]/.test(password) + var hasUpper = /[A-Z]/.test(password) + if (password == null || password == "") { + $(".errors li:nth-child(2)").show(); + $(".errors li:nth-child(3)").show(); + $(".errors li:nth-child(4)").show(); + } + if (password.length < 8) { + $(".errors li:nth-child(2)").show(); + } + if (!hasUpper) { + $(".errors li:nth-child(3)").show(); + } + if (!hasDigit) { + $(".errors li:nth-child(4)").show(); + } + +} diff --git a/validator_coffee.coffee b/validator_coffee.coffee new file mode 100644 index 0000000..e967dbb --- /dev/null +++ b/validator_coffee.coffee @@ -0,0 +1,29 @@ +form = document.forms["sign_in"]; + +$(document).ready -> + $(".errors").children().hide() + + $("form").submit -> + validateEmail() + validatePassword() + return + +validateEmail = () -> + email = /(^[a-z0-9_])+(.|[_])*([a-z0-9])*@([a-z]+).+/ + validateEmail = form.elements[0].value + $(".errors li:nth-child(1)").show() if validateEmail == null || validateEmail == "" + $(".errors li:nth-child(1)").show() if !email + return + +validatePassword = () -> + password = form.elements[1].value + hasDigit = /[0-9]/.test(password) + hasUpper = /[A-Z]/.test(password) + if password == null || password == "" + $(".errors li:nth-child(2)").show() + $(".errors li:nth-child(3)").show() + $(".errors li:nth-child(4)").show() + $(".errors li:nth-child(2)").show() if password.length < 8 + $(".errors li:nth-child(3)").show() if !hasUpper + $(".errors li:nth-child(4)").show() if !hasDigit + return diff --git a/validator_coffee.js b/validator_coffee.js new file mode 100644 index 0000000..ea288d9 --- /dev/null +++ b/validator_coffee.js @@ -0,0 +1,48 @@ +// Generated by CoffeeScript 1.10.0 +(function() { + var form, validateEmail, validatePassword; + + form = document.forms["sign_in"]; + + $(document).ready(function() { + $(".errors").children().hide(); + return $("form").submit(function() { + validateEmail(); + validatePassword(); + }); + }); + + validateEmail = function() { + var email; + email = /(^[a-z0-9_])+(.|[_])*([a-z0-9])*@([a-z]+).+/; + validateEmail = form.elements[0].value; + if (validateEmail === null || validateEmail === "") { + $(".errors li:nth-child(1)").show(); + } + if (!email) { + $(".errors li:nth-child(1)").show(); + } + }; + + validatePassword = function() { + var hasDigit, hasUpper, password; + password = form.elements[1].value; + hasDigit = /[0-9]/.test(password); + hasUpper = /[A-Z]/.test(password); + if (password === null || password === "") { + $(".errors li:nth-child(2)").show(); + $(".errors li:nth-child(3)").show(); + $(".errors li:nth-child(4)").show(); + } + if (password.length < 8) { + $(".errors li:nth-child(2)").show(); + } + if (!hasUpper) { + $(".errors li:nth-child(3)").show(); + } + if (!hasDigit) { + $(".errors li:nth-child(4)").show(); + } + }; + +}).call(this);