Home > Article > Web Front-end > JavaScript form validation implementation code_javascript skills
This article mainly introduces the implementation code of JavaScript form verification in detail, which has certain reference value. Interested friends can refer to it
JavaScript can be used to process the data before it is sent to the server. Validate these input data in the HTML form
JavaScript Form Validation
JavaScript can be used to validate these input data in the HTML form before the data is sent to the server. Enter data to verify.
These typical form data verified by JavaScript are:
Has the user filled in the required fields in the form?
Is the email address entered by the user legal?
Has the user entered a valid date?
Has the user entered text in the numeric field?
Required (or required) items
The following function is used to check whether the user has filled in the required (or required) fields in the form Select) project. If the required field or required field is empty, then the warning box will pop up, and the function's return value is false, otherwise the function's return value is true (meaning there is no problem with the data) :
function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") {alert(alerttxt);return false} else {return true} } }
Here is the code along with the HTML form:
<html> <head> <script type="text/javascript"> function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") {alert(alerttxt);return false} else {return true} } } function validate_form(thisform) { with (thisform) { if (validate_required(email,"Email must be filled out!")==false) {email.focus();return false} } } </script> </head> <body> <form action="submitpage.htm" onsubmit="return validate_form(this)" method="post"> Email: <input type="text" name="email" size="30"> <input type="submit" value="Submit"> </form> </body> </html>
E-mail Verification
The following function checks whether the entered data conforms to the basic syntax of an email address.
means that the input data must contain the @ symbol and the period (.). At the same time, @ cannot be the first character of the email address, and there must be at least one period after @:
function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos<1||dotpos-apos<2) {alert(alerttxt);return false} else {return true} } }
The following is the complete code together with the HTML form:
【Related recommendations】
1. Javacript free video tutorial
2. bootstrap modal box Detailed explanation of remote examples
3. Detailed explanation of examples of JS implementing marquee scrolling effect
4. JS production of QQ chat message display and comment submission functions Code example
5. Single line of JS to implement mobile money format verification
The above is the detailed content of JavaScript form validation implementation code_javascript skills. For more information, please follow other related articles on the PHP Chinese website!