In the form program, a lot of Js code is needed on the page to verify the form, whether each field must be filled in, whether
can only be a number, whether ajax is required for remote verification, blablabla.
It would be very cumbersome to write them one by one, so our first goal is to build something similar to a DSL.
Use expressive statements instead of control statements to implement verification.
Secondly, if you write them one by one, there is another problem: all verifications must be passed before they can be submitted. However, individual verification will add a lot of extra control codes because of this feature
, and the verification will often be incomplete. So the second goal is to fully
integrate the entire verification process.
In the end, it cannot be a hard-coded implementation that cannot be expanded. Necessary scalability is still needed.
First, we need a class that can describe field validation
function Field(params){
this.field_id=params.fid; //ID of the field to be validated
this.validators=params.val; //array of validator objects
this. on_suc=params.suc; //The event executed when the verification is successful
this.on_error=params.err; //The event executed when the verification fails
this.checked=false; //Whether it passes Validation
}
We will discuss the validator object later. Next, we extend this class and add the validate method
Field.prototype.validate=function(){
//Loop through each validator
for(item in this .validators){
//Attach callback events for verification success and verification failure to the validator
this.set_callback(this.validators[item]);
//Execute the Validate method on the validator to verify Whether it complies with the rules
if(!this.validators[item].validate(this.data())){
break; //Stop once any validator fails
}
}
}
Add another method to get the field value:
//Method to get field value
Field.prototype.data=function(){
return document.getElementById(this.field_id).value;
}
The method set_callback to set the validator callback function is as follows:
Field.prototype.set_callback=function(val){
var self=this; //Change a name to store this, otherwise the closure of the function will overwrite this name
val .on_suc=function(){ //Method to verify successful execution
self.checked=true; //Set the field to verify successful execution
self.on_suc(val.tips); //Execute event to verify successful execution
}
val.on_error=function(){ //Method executed when verification fails
self.checked=false; //Field is set to verification failure
self.on_error(val.tips );//Events that fail to perform verification
}
}
Next let’s take a look at the validator. The validator is the class that actually performs the verification process. According to The general verification process can be classified into length verification (including required verification), regular expression verification, custom function verification, and Ajax remote verification, so we define these types A validator class, Ajax remote verification uses jQuery for convenience, other
parts are also useful: