Here's a simple script for an HTML form with JavaScript validation. It can be used to learn form validation.
<!DOCTYPE html>
<html>
<head>
<title>Simple Form Validation</title>
<script type="text/javascript">
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "" || x == null) {
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="/submit_form" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>
In this script, if you try to submit the form without entering a name, an alert box with the message "Name must be filled out" will appear, and the form won't be submitted until a name is provided.
Please replace "/submit_form" with your own server endpoint. This is where the form data will be sent when the form is submitted.
<!DOCTYPE html>
<html>
<head>
<title>Simple Form Validation</title>
<script type="text/javascript">
function validateForm() {
var x = document.forms["myForm"]["fname"].value;
if (x == "" || x == null) {
alert("Name must be filled out");
return false;
}
}
</script>
</head>
<body>
<form name="myForm" action="/submit_form" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>
</html>
In this script, if you try to submit the form without entering a name, an alert box with the message "Name must be filled out" will appear, and the form won't be submitted until a name is provided.
Please replace "/submit_form" with your own server endpoint. This is where the form data will be sent when the form is submitted.