Validating form inputs is an essential part of web development to ensure that the data submitted by users is accurate and formatted correctly. jQuery provides a simple way to validate form inputs with its built-in methods and event handling capabilities.
$(document).ready(function() {
$("form").on("submit", function(event) {
var isValid = true;
var email = $("#email").val();
var password = $("#password").val();
// Clear previous error messages
$(".error").remove();
// Validate email
if(!email) {
isValid = false;
$("#email").after('Email is required');
} else if(!isEmail(email)) {
isValid = false;
$("#email").after('Invalid email format');
}
// Validate password
if(password.length < 6) {
isValid = false;
$("#password").after('Password must be at least 6 characters');
}
// Prevent form submission if validation fails
if(!isValid) {
event.preventDefault();
}
});
function isEmail(email) {
var regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test(email);
}
});
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?