Dive into the world of cybersecurity with our podcast, exploring defense in depth strategies through real-life examples. Discover the power of client and server-side validation techniques, safeguarding digital landscapes one layer at a time.
Client side validation:
// Client-side validation for a registration form
function validateForm() {
var username =document.forms["registrationForm"]["username"].value;
var password =document.forms["registrationForm"]["password"].value;
// Check ifusername and password are not empty
if (username =="" || password == "") {
alert("Username and password must be filled out");
return false;// Prevent form submission
}
// Check ifpassword is at least 8 characters long
if(password.length < 8) {
alert("Password must be at least 8 characters long");
return false;// Prevent form submission
}
}
Server side validation:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// Middleware to parse incoming JSON requests
app.use(bodyParser.json());
// POST endpoint for user registration
app.post('/register', (req, res) => {
constusername = req.body.username;
constpassword = req.body.password;
//Server-side validation
if(!username || !password) {
returnres.status(400).json({ error: "Username and password are required"});
}
// Performfurther validation checks and save user data to the database
// ...
returnres.status(200).json({ message: "Registration successful" });
});
// Start the server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});