Saturday, February 21, 2026

Form validation in JavaScript

 Once the client or user entered all the necessary data and  press the submit button then all the data entered by the client must be correct and valid. Thus, form validation is the mechanism which allows client to enter only the correct information which can be send to the Server. JavaScript provides easy method for form validation at client side. Forma validation can be done in two ways: Basic validation which checks whether all the required fields are filled properly or not kept empty whereas, Data Format validation check whether the data entered into the form field are logically correct.

Basic form validation in JavaScript

<HTML>
<HEAD><TITLE>Form Validation</TITLE></HEAD>
<BODY>
<FORM>
<INPUT type = "text" id = "user">
<INPUT type = "password" id = "pass">
<INPUT type = "submit" onclick = "valid( )">
</FORM>
          
<SCRIPT>       
   
    function valid()
    {
        var user = document.getElementById("user").value;
        var pass = document.getElementById("pass").value;
        if(user == "" || pass == "")
        {
         alert("Please!! Fill up the form");
        }
    }

</SCRIPT>
</BODY>
</HTML>

No comments:

Post a Comment

Javascript program to calculate factorial

  In order to calculate factorial of a given number, user need to enter a number using HTML form.  After entering number user presses submit...