In this article, we learn about JavaScript From Validations. In validate the form data on the client computer before sending it to the web server.

There are two type of Validations in JavaScript

  • Client-side Validation
  • Server-Side Validation

Client-side Validation

JavaScript (web browser) Validate the form data on the client computer before sending it to the web server. If the data entered by a client is incorrect or simply missing, the server must return all the data to the client and request that the form resubmit the correct information. In client side there are two types,

  • Basic validation
  • Data format validation

Basic Validation

The form must be verified to ensure that all mandatory fields. This will require a rotation in each field on the form and check the data. It will check the data is entered or not in form, it is called the basic validation.

Data format validation

The data entered must be verified to the correct form and value. Your code is logic to test the correctness of the data. It checks the user entered data which is related to the field. For example, the name should be entered, the alphabet number must be entered digits only, it checks the data formats.

Server-side Validations

Validations can be done either on the server side or on the client side (web browser). The user input validation (or) verification takes place on the server side during a subsequent post session, called the server-side verification, and the user input verification is called the client-side validation (web browser) on the client side. JavaScript provides a way to check the form's data on the client's computer before sending it to the web server.

Advantage of Server-side validation

  • High Security
  • Validating if input is valid
  • protection against malicious users

Create a Structure of HTML Form

We will create a simple HTML form that will verify on the client side using JavaScript when the user clicks the Submit button. Well, create a login form named "loginform.html" and put the following code in it, then save it to your Pc.

Example

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" type="text/css" href="style1.css">
  6. <script type="text/javascript" src="formvalidations.js"></script>
  7. </head>
  8. <title>Login Form</title>
  9. </head>
  10. <body>
  11. <div class="text">LOGIN FORM</div>
  12. <br>
  13. <form name="form1" onsubmit="return (validate());" action="E:\MY Articles\javascript\Form validations\form.html">
  14. <table align="center" bgcolor="lightgreen">
  15. <th></th>
  16. <tr>
  17. <td><b>User Name </b></td>
  18. <td><input type="text" id="name" placeholder="Enter your name"></td>
  19. </tr>
  20. <tr></tr>
  21. <tr>
  22. <td><b>Password </b></td>
  23. <td><input type="password" id="Pass" placeholder="Enter your Password"></td>
  24. </tr>
  25. <tr></tr>
  26. <tr>
  27. <td align="center">
  28. <input type="submit" id="btn" value="submit">
  29. </td>
  30. </tr>
  31. </table>
  32. </form>
  33. </body>
  34. </html>

Building the Form Validation Script

Now we are going to create a JavaScript file that contains our complete validation script. Now, create a JavaScript file named "formvalidator.js", and then save the previous HTML file in the same place where you saved it.

Example

  1. function validate() {
  2. var name1 = document.getElementsById("name").value;
  3. var pass = document.getElementsById("pass").value;
  4. //condtions
  5. if (name1 == "")
  6. {
  7. alert("Enter the Name");
  8. document.login1.name.focus();
  9. return false;
  10. }
  11. //check the name
  12. if (!isNaN name1 == "")
  13. {
  14. alert("Enter alphabet only");
  15. document.login1.name.value="";
  16. document.login1.name.focus();
  17. return false;
  18. }
  19. //password
  20. if (pass == "")
  21. {
  22. alert("Enter the password");
  23. document.login1.pass.focus();
  24. return false;
  25. }
  26. //check password length
  27. if (pass <6)
  28. {
  29. alert("Enter atleast 6 Characters");
  30. document.login1.pass.value="";
  31. document.login1.pass.focus();
  32. return false;
  33. }
  34. return true; //all the field user will be entered means goto next page
  35. }

Adding Stylesheet to the Form

Finally, create a file named "style.css", and then save the previous two files in the same location. That's it, now open the "loginform.html" file in a web browser and try to fill in some data and see how the script responds when incorrect data is entered in a form field.

Example

  1. body
  2. {
  3. margin: 0;
  4. padding: 0;
  5. background-color:#6abadeba;
  6. font-family: 'Arial';
  7. }
  8. .text{
  9. font-size: 2em;
  10. font-weight: bold;
  11. text-align: center;
  12. background: linear-gradient(blue, #fa709a);
  13. -webkit-text-fill-color:transparent;
  14. -webkit-background-clip: text;
  15. }
  16. .form{
  17. width: 382px;
  18. overflow: hidden;
  19. margin: auto;
  20. margin: 20 0 0 450px;
  21. padding: 80px;
  22. background: #23463f;
  23. border-radius: 15px ;
  24. }
  25. #name{
  26. width: 300px;
  27. height: 30px;
  28. border: none;
  29. border-radius: 3px;
  30. padding-left: 8px;
  31. }
  32. #pass{
  33. width: 300px;
  34. height: 30px;
  35. border: none;
  36. border-radius: 3px;
  37. padding-left: 8px;
  38. }
  39. #btn{
  40. width: 310px;
  41. height: 30px;
  42. border: none;
  43. border-radius: 5px;
  44. padding-left: 0px;
  45. color: blue;
  46. margin: auto;
  47. }

Output

In this image, there is no data entered. This will show an alert message.
In this image, the name field the entered data is a number that does not match it will show the alert message enter alphabet only.
Password less than six characters, you must set a password at least six characters.
Now, Enter the correct username and password, it will goto to the next page.
After, you entered all the data is correct. click the submit button goto next page.

Summary

In this article, we have seen form validations using JavaScript. I hope this article is useful to you.