HTML form with JavaScript validation | Web Scripts | Crax

Welcome To Crax.Pro Forum!

Check our new Marketplace at Crax.Shop

   Login! SignUp Now!
  • We are in solidarity with our brothers and sisters in Palestine. Free Palestine. To learn more visit this Page

  • Crax.Pro domain has been taken down!

    Alternatives: Craxpro.io | Craxpro.com

HTML form with JavaScript validation

HTML form with JavaScript validation

LV
0
 

arobnu62

Member
Joined
Jul 19, 2023
Threads
2
Likes
0
Credits
190©
Cash
0$
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.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Top Bottom