Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript HTML DOM Forms

JS HTML DOM Forms

HTML form validation can be done by JavaScript.

JavaScript Form Validation

If a form field (fname) is empty, this function alerts a message, and returns false, to prevent the form from being submitted.

					 
        
          <html>
            <body>
              <form name="myForm" action="#" onsubmit="return validateForm()" method="get">
                Name: <input type="text" name="fname">
                <input type="submit" value="Submit">
              </form>
              <script>
                function validateForm() {
                  let x = document.forms["myForm"]["fname"].value;
                  if (x == "") {
                    alert("Name must be filled out");
                    return false;
                  }
                }
              </script>
            </body>
          </html>
        
      
Automatic HTML Form Validation

HTML form validation can be performed automatically by the browser:

If a form field (fname) is empty, the required attribute prevents this form from being submitted.

					 
        
          <form action="#" method="get">
            Name: <input type="text" name="fname" required>
            <input type="submit" value="Submit">
          </form>
        
      
Data Validation

Data validation is the process of ensuring that user input is clean, correct, and useful.

Typical validation tasks are:

  • has the user filled in all required fields?
  • has the user entered a valid date?
  • has the user entered text in a numeric field?

Most often, the purpose of data validation is to ensure correct user input.

Validation can be defined by many different methods, and deployed in many different ways.

Server side validation is performed by a web server, after input has been sent to the server.

Client side validation is performed by a web browser, before input is sent to a web server.

HTML Constraint Validation

HTML5 introduced a new HTML validation concept called constraint validation.

HTML constraint validation is based on:

  • Constraint validation HTML Input Attributes
  • Constraint validation CSS Pseudo Selectors
  • Constraint validation DOM Properties and Methods
Constraint Validation HTML Input Attributes
Attribute Description
disabled Specifies that the input element should be disabled
max Specifies the maximum value of an input element
min Specifies the minimum value of an input element
pattern Specifies the value pattern of an input element
required Specifies that the input field requires an element
maxlength Specifies the maximum character length of an input element
minlength Specifies the minimum character length of an input element
type Specifies the type of an input element
Constraint Validation CSS Pseudo Selectors
Selector Description
:disabled Selects input elements with the "disabled" attribute specified
:invalid Selects input elements with invalid values
:optional Selects input elements with no "required" attribute specified
:required Selects input elements with the "required" attribute specified
:valid Selects input elements with valid values

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved