Skip to content

Commit

Permalink
[#118543823] Adds Email validation
Browse files Browse the repository at this point in the history
  • Loading branch information
andela-gnyenyeshi committed Apr 29, 2016
1 parent 4f13304 commit 4de1bc2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
6 changes: 3 additions & 3 deletions public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $(function () {
contentType: 'application/json',
success: function(response) {
var data = response.members;
console.log(data.email);
// console.log(data.email);
// Clear the tbody
$('tbody').html('');
// Loop and append
Expand All @@ -30,7 +30,6 @@ $(function () {
// Get the value from form
var attendee = $('#attendee').val();
var email = $('#email').val();
console.log(email);
// If user has not filled form
if (!attendee || !email) {
Materialize.toast('Please fill in both fields', 4000);
Expand All @@ -42,8 +41,9 @@ $(function () {
contentType: 'application/json',
data: JSON.stringify({name: attendee, email:email}),
success: function(response) {
console.log(response);
$('#get-button').trigger('click');
Materialize.toast('Successfully added new user', 4000);
Materialize.toast(response.message, 4000);
}
});
}
Expand Down
30 changes: 21 additions & 9 deletions server/controllers/member.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@
module.exports = {
// Function to create a new member
create: function(req, res) {
// Instance of a model
var member = new Member();
member.name = req.body.name;
member.email = req.body.email;
member.save(function(err, savedMember) {
if (err) {
console.log(err);
return res.status(500).send(err);
// Check if Email exists
Member.findOne(req.body.email, function(err, result) {
if (result) {
return res.status(409).json({
message: 'This Email already exists'
});
} else {
return res.status(200).send(savedMember);
// Instance of the model Member
var member = new Member();
member.name = req.body.name;
member.email = req.body.email;

member.save(function(err, savedMember) {
if (err) {
console.log(err);
return res.status(500).send(err);
} else {
return res.status(200).json({
message: 'Successfully added new user'
});
}
});
}
});
},
Expand Down

0 comments on commit 4de1bc2

Please sign in to comment.