Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 77 additions & 4 deletions validator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,80 @@
function Validator () {
this.length = 8;
this.capitals = 1;
this.numbers = 1;
this.email_error = '.errors li:nth-child(1)'
this.length_error = '.errors li:nth-child(2)'
this.caps_error = '.errors li:nth-child(3)'
this.nums_error = '.errors li:nth-child(4)'
}

//insert your code here

$(function(){
Validator.prototype.validateEmail = function(email, selector) {
if (email.search(/^[^@]+@[^@]+\.[^@]+$/) == -1) {
$(selector).show()
return false;
} else {
return true;
}
}

//insert your code here
Validator.prototype.validatePWLength = function(password, selector) {
if (password.length < 8) {
$(selector).show()
return false;
} else {
return true;
}
}

Validator.prototype.validatePWCAPS = function(password, selector) {
if (password.search(/[A-Z]/) == -1) {
$(selector).show()
return false;
} else {
return true;
}
}

Validator.prototype.validatePWNums = function(password, selector) {
if (password.search(/\d/) == -1) {
$(selector).show();
return false;
} else {
return true;
}
}

Validator.prototype.validate = function(email, password) {
var tests = [this.validateEmail(email, this.email_error),
this.validatePWLength(password, this.length_error),
this.validatePWCAPS(password, this.caps_error),
this.validatePWNums(password, this.nums_error)]

if (tests.join("").search(/false/) == -1) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. I was thinking of putting Underscore in here. Would probably help with some of the other refactoring too.

return true
} else {
return false
}
}

function hideErrors() {
$('.errors li').hide()
}

var form = '.container form'

$(document).ready(function(){
var validator = new Validator;
hideErrors();
$(form).on('submit',function(e){
hideErrors();

var email = $($(this).children()[0]).val();
var password = $($(this).children()[2]).val();

if (!validator.validate(email, password)) {
e.preventDefault();
}
});
});