diff --git a/validator.coffee b/validator.coffee new file mode 100644 index 0000000..5f99510 --- /dev/null +++ b/validator.coffee @@ -0,0 +1,44 @@ +check_email = (email) -> + /[a-zA-Z0-9-._]+@[a-zA-Z0-9-._]+\.[a-zA-Z0-9-._]/.test(email) + +check_long_enough = (password) -> + password.length >= 8; + +check_is_capital = (password) -> + /[A-Z]/.test(password) + +check_is_number = (password) -> + /\d/.test(password) + +$('.errors li').hide() + +$ -> + $("form").submit (event) -> + + is_valid = false + $(".errors li").hide() + + email = $("input[placeholder='Email']").val() + password = $("input[placeholder='Password']").val() + + if !check_email(email) + is_valid = false + $(".errors li:nth-child(1)").show() + + if !check_long_enough(password) + is_valid = false + $(".errors li:nth-child(2)").show() + + if !check_is_capital(password) + is_valid = false + $(".errors li:nth-child(3)").show() + + if !check_is_number(password) + is_valid = false + $(".errors li:nth-child(4)").show() + + if !is_valid + event.stopPropagation() + event.preventDefault() + return + return diff --git a/validator.js b/validator.js index 860dab4..c212a3e 100644 --- a/validator.js +++ b/validator.js @@ -1,7 +1,53 @@ +// Generated by CoffeeScript 1.9.3 +(function() { + var check_email, check_is_capital, check_is_number, check_long_enough; - //insert your code here - -$(function(){ + check_email = function(email) { + return /[a-zA-Z0-9-._]+@[a-zA-Z0-9-._]+\.[a-zA-Z0-9-._]/.test(email); + }; - //insert your code here -}); + check_long_enough = function(password) { + return password.length >= 8; + }; + + check_is_capital = function(password) { + return /[A-Z]/.test(password); + }; + + check_is_number = function(password) { + return /\d/.test(password); + }; + + $('.errors li').hide(); + + $(function() { + $("form").submit(function(event) { + var email, is_valid, password; + is_valid = false; + $(".errors li").hide(); + email = $("input[placeholder='Email']").val(); + password = $("input[placeholder='Password']").val(); + if (!check_email(email)) { + is_valid = false; + $(".errors li:nth-child(1)").show(); + } + if (!check_long_enough(password)) { + is_valid = false; + $(".errors li:nth-child(2)").show(); + } + if (!check_is_capital(password)) { + is_valid = false; + $(".errors li:nth-child(3)").show(); + } + if (!check_is_number(password)) { + is_valid = false; + $(".errors li:nth-child(4)").show(); + } + if (!is_valid) { + event.stopPropagation(); + event.preventDefault(); + } + }); + }); + +}).call(this); diff --git a/validatorjs.js b/validatorjs.js new file mode 100644 index 0000000..c251893 --- /dev/null +++ b/validatorjs.js @@ -0,0 +1,56 @@ +$(".errors li").hide(); + + +$(function(){ + + + $("form").submit(function(event) { + var is_valid = false; + $(".errors li").hide(); + + var email = $("input[placeholder='Email']").val(); + var password = $("input[placeholder='Password']").val(); + + if(!check_email(email)){ + is_valid = false; + $(".errors li:nth-child(1)").show(); + } + + if(!check_long_enough(password)){ + is_valid = false; + $(".errors li:nth-child(2)").show(); + } + + if(!check_is_capital(password)){ + is_valid = false; + $(".errors li:nth-child(3)").show(); + } + + if(!check_is_number(password)){ + is_valid = false; + $(".errors li:nth-child(4)").show(); + } + + if(!is_valid){ + event.stopPropagation(); + event.preventDefault(); + } + + }); +}); + +function check_email(email) { + return /[a-zA-Z0-9-._]+@[a-zA-Z0-9-._]+\.[a-zA-Z0-9-._]/.test(email); +} + +function check_long_enough(password) { + return password.length >= 8; +} + +function check_is_capital(password) { + return /[A-Z]/.test(password); +} + +function check_is_number(password) { + return /\d/.test(password); +}