Before we discuss form validation in depth, let us first think about the true meaning of form validation. At its core, form validation is a system that detects invalid control data and flags these errors for end users. In other words, form validation performs a series of checks on the form before it is submitted to the server and notifies the user to correct errors.
But what is form validation really?
is an optimization.
The reason why form validation is an optimization is because the form validation mechanism alone is not enough to ensure that the form data submitted to the server is correct and valid. On the other hand, form validation is designed to make web applications throw errors faster. In other words, it's best to use the browser's built-in handling mechanism to notify the user that a web page contains invalid form control values. In the past, data traveled around the network only for the server to notify the user that he had entered incorrect data. If the browser is fully capable of allowing errors to be caught before they leave the client, then we should take advantage of this.
However, the browser's form checking is not enough to handle all errors.
Having said that, HTML5 introduces eight methods for verifying the correctness of data in form controls. Let's look at them in turn, but first we'll introduce the ValidityState object used to feedback validation status.
In browsers that support Html5 form validation, the ValidityState object can be accessed through the form control:
var valCheck = document.myForm.myInput.validity;
This line of code obtains the ValidityState object of the form element named myInput. The object contains references to all eight verification states, as well as the final verification result.
The calling method is as follows:
valCheck.valid
After execution, we will get a Boolean value, which indicates whether the form control has passed all validation constraints. The valid attribute can be regarded as the final verification result: if all eight constraints are passed, the valid attribute is true. Otherwise, as long as one constraint fails, the valid flag is false.
As mentioned before, there are eight possible validation constraints for any form element. Each condition has a corresponding attribute name in the ValidityState object, which can be accessed in the appropriate way. Let's analyze them one by one to see how they are associated with the form controls and how to check them based on the ValidityState object:
1, valueMissing
Purpose: Ensure the value in the form control Filled in.
Usage: Set the required attribute to true in the form control.
Example:
<input type="text" name="myText" required>
Detailed description: If the form control sets the required attribute, the control will remain in an invalid state until the user fills in the value or fills in the value through code call. For example, an empty text input box fails the required check unless any text is entered into it. When the input value is empty, valueMissing returns true.
2. typeMismatch
Purpose: To ensure that the control value matches the expected type (such as numbe, email, URL, etc.).
Usage: Specify the type attribute value of the form control.
Example:
<input type="email" name="myEmail">
Detailed description: The special form control type is not only used to customize the mobile phone keyboard. If the browser can recognize that the input in the form control does not comply with the corresponding type rules, such as There is no @ symbol in the email address, or the input value of the number type control is not a valid number, then the browser will mark the control to indicate a type mismatch. Regardless of the error condition, typeMismatch will return true.
3. patternMismatch
Purpose: Verify whether the input is in a valid format according to the format rules set on the form control.
Usage: Set the pattern attribute on the form control and assign appropriate matching rules.
Example:
<input type="text" name="creditcardnumber" pattern="[0-9]{16}" title="A credit card number is 16 digits with no spaces or dashes">
Detailed description: The pattern attribute provides developers with a powerful and flexible way to set a regular expression validation mechanism for the form's control value. When the pattern attribute is set for a control, patternMismatch will return a true value as long as the value of the input control does not comply with the pattern rules. From the perspective of user guidance and technical reference, you should set the title attribute in the form control containing the pattern attribute to illustrate the role of the rule.
4, tooLong
Purpose: To avoid input values containing too many characters.
Usage: Set the maxLength attribute on the form control.
Example:
<input type="text" name="limitedText" maxLength="140">
Details: If the length of the input value exceeds maxLength, the tooLong feature will return true. Although form controls usually limit the maximum length of user input, there are cases where the maximum length can be exceeded, such as by setting it programmatically.
5. rangeUnderflow
Purpose: Limit the minimum value of numerical controls.
Usage: Set the min attribute for the form control and give it the minimum allowed value.
Example:
<input type="range" name="ageCheck" min="18">
Detailed description: In a form control that requires numerical range checking, the value is likely to be temporarily lower than the set lower limit. At this time, the rangeUnderflow property of ValidityState will return true.
6. rangeOverflow
Purpose: Limit the maximum value of numeric controls.
Usage: Set the max attribute for the form control and give it the maximum allowed value.
Example:
<input type="range" name="kidAgeCheck" max="12">
详细说明:与rangeUnderflow类似,如果一个表单控件的值比max更大,特性将返回true。
7、stepMismatch
目的:确保输入值符合min、max及step即设置。
用法:为表单控件设置step特性,指定数值的增量。
示例:
<input type="range" name="confidenceLevel" min="0" max="100" step="5">
详细说明:此约束条件用来保证数值符合min、max和step的要求。换句话说,当前值必须是最小值与step特性值的倍数之和。例如,范围从0到100,step特性值为5,此时就不允许出现17,否则stepMismatch返回true值。
8、customError
目的:处理应用代码明确设置及计算产生的错误。
用法:调用setCustomValidity(message)将表单控件置于customError状态。
示例:
passwordConfirmationField.setCustomValidity("Password values do not match.");
详细说明:浏览器内置的验证机制不适用时,需要显示自定义验证错误信息。当输入值不符合语义规则时,应用程序代码应设置这些自定义验证消息。
自定义验证消息的典型用例是验证控件中的值是否一致。例如,密码和密码确认两个输人框的值不匹配。只要定制了验证消息,控件就会处于无效状态,并且customError返回true。要清除错误,只需在控件上调用setCustomValidity("")即可。
以上就是详解HTML5中表单验证的8种方法介绍 的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!