Skip to content

Commit

Permalink
add more complex client-side validation #74
Browse files Browse the repository at this point in the history
  • Loading branch information
jwld committed May 30, 2017
1 parent ec5e4e9 commit 1c83401
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 40 deletions.
1 change: 0 additions & 1 deletion database/mongo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const MongoClient = require('mongodb').MongoClient;
require('env2')('./config.env');

MongoClient.connect(process.env.DATABASE_URL, (err, db) => {
if (err) return console.log(err);
Expand Down
5 changes: 3 additions & 2 deletions public/scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ var indexModule = (function() {
}
xhr.open(method, url);

if (method === 'POST' || method === 'PUT') {
xhr.send(data);
if (method === 'POST') {
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
} else {
xhr.send();
}
Expand Down
48 changes: 25 additions & 23 deletions public/scripts/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,38 @@
name: e.target.elements.name.value,
email: e.target.elements.email.value,
password: e.target.elements.password.value,
confirm: e.target.elements.confirm.value
confirmation: e.target.elements.confirm.value
}

function validate(data) {
// check inputs aren't empty
for (var key in data) {
if (!data[key].trim()) {
indexModule.showMessage('Missing information: ' + key);
return false;
}
}

// check email contains @
if (!/@/.test(data.email) || data.email[data.email.length - 1] === '@') {
indexModule.showMessage('Email address is invalid');
return false;
}
// send data to server if it passes validation
if (validate(data)) {
indexModule.makeRequest('/add-user', 'POST', data, function(err) {
if (err) return console.log(err);
});
}
});

// check both passwords match
if (data.password !== data.confirm) {
indexModule.showMessage('Passwords don\'t match');
function validate(data) {
// check inputs aren't empty
for (var key in data) {
if (!data[key].trim()) {
indexModule.showMessage('Missing information: ' + key);
return false;
}
}

return true;
// check email contains @
if (!/@/.test(data.email) || data.email[0] === '@' || data.email[data.email.length - 1] === '@') {
indexModule.showMessage('Email address is invalid');
return false;
}

// send data to server if it passes validation
if (validate(data)) {
console.log('Form validated, sending to server.');
// check both passwords match
if (data.password !== data.confirmation) {
indexModule.showMessage('Passwords don\'t match');
return false;
}
});

return true;
}
})();
4 changes: 4 additions & 0 deletions public/style/sass/_form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@
}
}
}

.input-focus {
border-color: $main !important;
}
10 changes: 2 additions & 8 deletions src/routes/add_user.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
const MongoClient = require('mongodb').MongoClient;
require('env2')('./config.env');

module.exports = (req, res) => {
module.exports = (req) => {
MongoClient.connect(process.env.DATABASE_URL, (err, db) => {
if (err) return console.log(err);

return db.collection('users').insertOne({
name: req.body.name,
email: req.body.email,
password: req.body.password
}, (error, result) => {
}, (error) => {
if (error) return console.log(error);
console.log(result);
return db.close();
});
});

res.render('register', {
title: 'Register'
});
};
3 changes: 2 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ app.engine('hbs', hbs({

app.set('view engine', 'hbs');

app.use(bodyParser.urlencoded({ extended: false })); // required to process post data
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'));
app.use(router);

Expand Down
1 change: 1 addition & 0 deletions src/start.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const app = require('./server.js');
require('env2')('./config.env');

app.listen(3000, () => {
console.log('Server running at 3000');
Expand Down
10 changes: 5 additions & 5 deletions views/register.hbs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{{>header}}

<div class="page-content">
<form class="registration-form" id="register-form">
<form class="registration-form" id="register-form" autocomplete="off">
<div class="form-input-wrap">
<label>Name
<input type="text" name="name" autocomplete="off">
<input type="text" name="name">
</label>

<label>Email Address
<input type="text" name="email" autocomplete="off">
<input type="text" name="email">
</label>

<label>Password
<input type="password" name="password" autocomplete="off">
<input type="password" name="password">
</label>

<label>Confirm Password
<input type="password" name="confirm" autocomplete="off">
<input type="password" name="confirm">
</label>
</div>
<input id="submit" type="submit" value="SUBMIT">
Expand Down

0 comments on commit 1c83401

Please sign in to comment.