Write a program to demonstrate an HTML Form Validation Example.

1
2
    <!DOCTYPE html>
3
    <html>
4
    <head>
5
        <script>
6
            function validateForm() {
7
                var x = document.forms["myForm"]["fname"].value;
8
                if (x == "") {
9
                    alert("Name must be filled out");
10
                    return false;
11
                }
12
            }
13
        </script>
14
    </head>
15
    <body>
16
        <form name="myForm" onsubmit="return validateForm()">
17
            Name: <input type="text" name="fname">
18
            <input type="submit" value="Submit">
19
        </form>
20
    </body>
21
    </html>
22
    

Output

1
2
        No Output Available
3
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

Core Concept of HTML Form Validation Example

To check the user data at the browser level before sending it to the server, this is known as client-side validation or validation on the front end.

In this code:

  • A JavaScript method validateForm() is defined and executed before form submission.
  • This method checks whether the name field is empty or not using document.forms[“myForm”][“fname”].value.
  • If the field is empty, then it shows an error as a Windows alert and returns false that means form will not be submitted. So, the user can enter/fill in the name and press the submit button again.

Why is it used?

  • To prevent sending wrong data, incomplete, or empty data to the server.
  • To show the user an error to fill out the form without reloading the page.

Real-life Applications

  • Sign-up & Login Forms: To ensure the username, password, and email are not empty.
  • Checkout and Payment Forms: To check the mandatory fields or credit card details like card number, expiry date, CSV, etc.
  • There are endless real applications of form validations, but we added only these two validations.

Key Benefits of this Program

  • Save server load: The wrong data is not sent to the server, so the processing of wrong data is also removed.
  • Instant user error feedback: If the user enters the wrong data, then the user can receive the error message instantly on the screen.

Limitations of this Program

  • Space Problem: Users can add extra or unnecessary spaces to text-based fields, so you need to add the trim() JavaScript method to each field.
  • Security (Client-Side Limitations): There, validations can be turned off at the browser level by the end user. So, server-side validation is a must.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.