Validate the form raised by the user because it can have blank values. So, validation is a must to authenticate users.
JavaScript library provides a way to validate the form on the client-side validation so data processing will be faster other than server-side validation. Most developers prefer JavaScript form validation.
So, in JavaScript, we can validate name, password, email, date, mobile numbers, and more fields.
index.php
<html>
<head>
<title>how to validate form in javascript?</title>
<script>
function validateform() {
var user_name = document.myform.user_name.value;
var user_address = document.myform.user_address.value;
if (user_name == null || user_name == "") {
alert("User Name can't be blank");
return false;
}
if (user_address == null || user_address == "") {
alert("User Address can't be blank");
return false;
}
}
</script>
</head>
<body>
<form method = "post" name="myform" onsubmit="return validateform()" >
<table width = "400" border = "0" cellspacing = "1" cellpadding = "2">
<tr>
<td width = "100">User Name</td>
<td><input name = "user_name" type = "text" placeholder="User Name"></td>
</tr>
<tr>
<td width = "100">User Address</td>
<td><input name = "user_address" type = "text" placeholder="User Address"></td>
</tr>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td>
<input name = "submit" type = "submit" value = "Add User">
</td>
</tr>
</table>
</form>
</body>
</html>
I hope it can help you...