Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement form validation with JavaScript

Shulou Source: shulou.com Published: 2022-06-01 07:18:58 10月01日 Update

This article will explain in detail how to achieve form verification in JavaScript. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

HTML forms (form) are usually used to collect user information, such as name, email address, location, age, and so on.

However, it is quite possible that some users may not enter the data you expect. HTML form validation can be done through JavaScript.

To avoid unnecessary pressure on server resources, you can use JavaScript to validate form data on the client (user system). Incorrect information is not submitted to the background server-this is called client authentication. This article will introduce this kind of verification

There are generally two ways of form validation.

Client-side verification: JS is executed directly on the client side for verification, and there is no interaction with the server during the verification process.

Server-side verification: the page sends the verification information back to the server, which verifies, and sends the verification result back to the client.

Both authentication are necessary, because the client authentication security is not too high, but it can prevent more than 80% of users from misoperation, reduce the pressure on the server side, and the speed is very fast, and the user experience is high. Do not think that with the client-side verification, there is no need for the server-side verification, the client-side verification is easy to bypass, the server-side verification security is relatively high, so the general verification two verifications need to be written. ]

Example 1, a simple example

Simple example of form validation: function validateForm () {var x=document.forms ["myForm"] ["fname"] .value; if (x==null | | x _ name = "") {/ / alert ("name must be filled in"); usernameerror [XSS _ clean] = "name must be filled in"; return false;} else {usernameerror [XSS _ clean] = "correct"; return true;}}

Tags: Authentication form server user service client client end input information name age example user name article security two pressure security effect Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi MySQL Shulou Tech Info Huawei