Form verification has always been the most basic and the most difficult part of web development. Say is the basis, I believe how many people engaged in web development The first thing is to learn if you are based on JS, jquery implementation forms validation work. 10 years ago, and even 5 years ago, JavaScript was a language that was despised by most engineers, and many people thought that JavaScript could do a part of the work of form verification at best. This is how the form check is a basic thing; it is difficult to do because the form check is often a lot of work, it can be said to be a less technical content but very labor-intensive work, and in some of the more interactive friendly requirements of the project, the form check is more trouble.
So we often rely on a number of ready-made form verification framework, such as: Jquery-validation, Validation-engine, easy-ui-validation and so on. But these libraries can only be used for fixed occasions, the form of hints is also quite fixed, in the current template-based era, the form of the validation of the logical part of the form is more important than the hint.
Analysis of the above JS Library, you can find that they have a common feature, is to adopt a strategy model, the different forms of validation rules encapsulated into a policy class, improve the reusability of the verification logic; also provides the form of a "rule string" that allows the user to create a rule instance with a simple string to complete the validation of the form's entry. This design makes form validation a physical activity into a configuration item, greatly reducing the amount of code associated with form validation. Unfortunately, the above controls are all using this logic, but the foothold is a hint of the form, and its logical part cannot be used alone.
Therefore, based on the above control of the form validation features, the author developed a form check library, more accurate is the form Check Logic library--my-validation, this library only provides the logic of form verification, using a simple API can be complex form verification process simple, easy to verify the logical reuse, and returns a list of error results with prompt statements, which facilitates embedding in any frame and completing the logical part of form validation.
Installation method:
Used in Webpack:
NPM install git://github.com/laden666666/my-validation--save
Then you can use the Reqiure reference directly
var myvalidation = require ("my-validation");
If it is a browser environment Direct script tag refers to my-validation.js or my-validation.min.js file.
How to use: 1. Check against the rule string:
A total of two two overloaded APIs are available:
Myvalidation.validation (rulestringstring, valuestring, Isstringpath)
Parameters |
parameter Description |
Rulestringstring |
string of rules |
valuestring |
The string to validate |
Myvalidation.validation (Rulestringjson, values, Isstringpath)
Parameters |
parameter Description |
Rulestringjson |
A collection of strings for the rule |
Values |
The collection of strings to validate |
Isstringpath |
Enable the path of the child object key to do key mode, verify that the path corresponds to the properties of the child object |
the string of rules is the core of the checksum, the format is "rule 1 [Parameter 1, parameter 2 ...]; Rule 2 Names [parameter 1, Parameter 2 ...] ...”。
For example:"required;minsize[5]" means to use required rule checksum, check non-null, and then use minSize rule checksum, the parameter is 5, the string length can not be less than 5
The special characters in the regular string are ";", "," two, need to be escaped, escape characters are ";;", ";,"
A string can be verified directly when used, such as:
var result = Myvalidation.validation ("required;sizemax[16", "Test");
You can also perform a checksum of an object based on a JSON, and support the function of the applet's key that uses the path of the child object key, such as:
var result = myvalidation.validation ({ "user.name": "required;sizemax[16]",},{ User: { "test" }}, true)
The result returned by Myvalidation.validation is an array of validation rule results.
2. Register a custom rule:
The validation rule is equivalent to the implementation of a policy pattern, and if the rule cannot be customized, the calibration system is equivalent to a bird with a broken wing, with no freedom whatsoever. So it's important to be able to provide APIs that dynamically extend the rules.
Myvalidation.registerrule (name, VALIDATIONFN, msg)
Parameters |
parameter Description |
Name |
Name of the rule |
Validationfn |
Check function |
Msg |
Default error prompt statement, support for string or function |
Examples of dynamically generated default error prompts are:
function (Value, object, count) { return !! Value && value.length >= parseint (count);},function (value, object, count) {
return "minimum input" + count + "number of characters";});
Example of specifying default error hints:
function (Value, object) { return !! Value &&/^[-+]?\d+$/"must be an integer");
Check function is the core of the form check, through the above two examples we can check the definition of the function of the way, his specific parameters are as follows:
Parameters |
Parameter description |
Value |
The value to validate |
Object |
The checksum object, including the checksum string, and the user's other custom properties |
Params |
The parameter list of the checksum string, which is the rest parameter. such as Xx[a,b,c], here will parse out 3 parameters, respectively: "A", "B", "C" |
If the checksum fails, it must return false or the return result generated by the call to Myvalidation.result, otherwise it will be considered a validation success.
3. The custom return error prompt:
Sometimes it is necessary to generate a dynamic return error when validating, instead of returning the default error, you can use the following APIs:
Myvalidation.result (result, msg)
Parameters |
parameter Description |
Result |
Boolean, the result of the checksum, which indicates that the validation failed only when false |
Msg |
Validation of a failed string that indicates that the precedence of this string is higher than the default string |
Such as:
function () { return myvalidation.result (false, "custom error prompt", "This is the default prompt, will be overwritten");
Default Check Library:
Parameters |
parameter Description |
Example |
Required |
Must fill in |
Required |
MinSize |
Requires that the string length is not greater than the specified value |
MINSIZE[5] |
MaxSize |
Requires that the string length is not less than the specified value |
MAXSIZE[5] |
Min |
Must be a number and require no less than the specified value |
MIN[5] |
Max |
Must be a number and require no more than the specified value |
MAX[5] |
Number |
Must be a number |
Number |
Integer |
Must be an integer |
Integer |
Currently only a few of the authors will use the rules, and other rules can be registered by Myvalidation.registerrule.
Create a common form check logical library