Home > Web Front-end > JS Tutorial > How to fully validate a form using regular expressions and javascript

How to fully validate a form using regular expressions and javascript

伊谢尔伦
Release: 2016-11-22 14:21:53
Original
961 people have browsed it

Please save the following javascript code into a single js file when using it.
1. Form requirements

<form name="formname" onSubmit="return validateForm(this)"></form>
Copy after login

will verify all the following types of fields in the form in sequence. All verifications will remove leading and suffix spaces. Please note that they are case-sensitive.
2. Empty value verification
Adding the emptyInfo attribute to any field in the form will verify whether the field is empty (can be used at the same time as the general verification method of maximum length verification).
Without this attribute, it is considered that this field allows null values.
For example:
3. Maximum length verification (can be used together with null value verification and general verification methods):
< input type="text" name="fieldNamename" maxlength="20" lengthInfo="The maximum length cannot exceed 20!">
or,