I haven't written JavaScript code for a long time, or I haven't written any articles for a long time. In the afternoon, I wrote a jQuery-based form verification plug-in. I declare that this plug-in has many problems, but I think it is necessary. Later, we continued to work with the new version. We first took out the plug-in prototype we wrote! I again stated that there are many plug-ins and they will be solved one by one in the future. Please do not say anything wrong. I hope you will give more suggestions and provide better comments.
I. analyze basic information about Form Verification
In our web development process, we will encounter various verifications. To sum up, we can basically divide it into several categories:
(1). Required? [This is very basic]
(2). Range verification in input parameters
(3). Comparison between input parameters and other control values
(4). Regular Expression verification of input parameters
2. required verification?
There are several situations:
(1) The input box obtains the focus prompt.
(2) error message about missing focus verification in the input box
(3) Correct prompt for missing focus verification in the input box
First, determine whether the input box is required, and then the actual location of the message is prompted.
The following parameters are determined based on the above situations:
Required: whether it is required, true or false, true indicates that it is required (*)
OnFocus: Text prompt for getting the focus (if you specify a style, add @ before the style name, so the text prompt cannot contain @ at the first letter @)
OnBlur: Text prompt with no focus (if a style is specified, add @ before the style name, so the text prompt cannot contain @ at the first letter) (Verification failure prompt)
OnSucces: Text prompt for successful verification (if a style is specified, add @ before the style name, so the text prompt cannot contain @ at the first letter @)
TipId: id of the control used to display the prompt information (*)
Combination example: {required: true, onFocus: "Required", onBlur: "@ error", onSucces: "Success", tipId: "tipid "}
Note: Some of the rules defined above may not be implemented and are gradually improved in the later stage.
The Code is as follows:
/**
* Check whether the input box is required.
* Input parameters:
* Required: indicates whether the parameter is required. Optional values: true and false. true indicates that the parameter is required (*)
* OnFocus: indicates the text prompt for getting the focus (if a style is specified, add @ before the style name, so the text prompt cannot contain @ at the first letter @)
* OnBlur: Text prompt with no focus (if a style is specified, add @ before the style name, so the text prompt cannot contain @ at the first letter) (Verification failure prompt)
* OnSucces: Text prompt for successful verification (if a style is specified, add @ before the style name, so the text prompt cannot contain @ at the first letter @)
* TipId: id of the control used to display the prompt information (*)
* Combination example: {required: true, onFocus: "Required", onBlur: "@ error", onSucces: "Success", tipId: "tipid "}
*/
$. Fn. extend ({
CheckRequired: function (inputArg ){
If (inputArg. required ){
If ($ (this). is ("input") | $ (this). is ("textarea ")){
// Bind a focus event
$ (This). bind ("focus", function (){
If (inputArg. onFocus! = Undefined ){
$ ("#" + InputArg.tipId).html (inputArg. onFocus );
}
});
// Bind an event with no focus
$ (This). bind ("blur", function (){
If ($ (this). val ()! = Undefined & $ (this). val ()! = ""){
$ ("#" + InputArg.tipId).html (inputArg. onSucces );
} Else {
$ ("#" + InputArg.tipId).html (inputArg. onBlur );
}
});
}
}
}
});
Results and test code:
The following message is displayed when the focus is obtained.
When the focus is lost, no prompt is displayed.
After the text information is entered, a message indicating successful results is displayed.
The test code is as follows:
The Code is as follows: