-
Notifications
You must be signed in to change notification settings - Fork 30
Esmith solution #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Esmith solution #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,68 @@ | ||
| // $() is an alias for $(document).ready() when you pass it a function | ||
|
|
||
| //insert your code here | ||
|
|
||
| $(function(){ | ||
| $(document).ready( function() { | ||
| $(".errors").children().hide(); | ||
|
|
||
| //insert your code here | ||
| $("form").submit( function() { | ||
| ValidateForm(); | ||
| }); | ||
| }); | ||
|
|
||
| function ValidateForm() { | ||
| ValidateEmail(); | ||
| ValidatePassword(); | ||
| }; | ||
|
|
||
| function PreventSubmission() { | ||
| event.preventDefault(); | ||
| }; | ||
|
|
||
| function ValidateEmail() { | ||
|
|
||
| var email = $("input:text").val(); | ||
| var email_regex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; | ||
| var invalid_format = !email_regex.test(email) | ||
|
|
||
| if (invalid_format) { | ||
| PreventSubmission; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does your validator prevent submission? I'm not sure it ever gets executed for a couple of reasons.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! You are totally right. Thanks for pointing that out. I constantly forget that JS is NOT Ruby. |
||
| $(".errors li:nth-child(1)").show(); | ||
| } | ||
| else { | ||
| $(".errors li:nth-child(1)").hide(); | ||
| }; | ||
| }; | ||
|
|
||
| function ValidatePassword() { | ||
| var password = $("input:password").val(); | ||
| var capital_regex = /[A-Z]/; | ||
| var num_regex = /[0-9]/; | ||
| var min_length = 8; | ||
| var too_short = password.length < min_length; | ||
| var no_capital_chars = !capital_regex.test(password) | ||
| var no_numbers = !num_regex.test(password) | ||
|
|
||
| if (too_short) { | ||
| PreventSubmission(); | ||
| $(".errors li:nth-child(2)").show(); | ||
| } | ||
| else { | ||
| $(".errors li:nth-child(2)").hide(); | ||
| }; | ||
|
|
||
|
|
||
| if (no_capital_chars) { | ||
| PreventSubmission(); | ||
| $(".errors li:nth-child(3)").show(); | ||
| } | ||
| else { | ||
| $(".errors li:nth-child(3)").hide(); | ||
| }; | ||
|
|
||
| if (no_numbers) { | ||
| PreventSubmission(); | ||
| $(".errors li:nth-child(4)").show(); | ||
| } | ||
| else { | ||
| $(".errors li:nth-child(4)").hide(); | ||
| }; | ||
| }; | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! My suggestion would be to pull the function definitions for ValidateForm and all of the method's it calls outside of the document ready callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally makes sense! Done.