The technical principles and design ideas of the "PH verification" framework in the Java class library
The PH verification framework is an open source Java library used to verify the user input.It provides a simple and flexible way to verify the data submitted by the user to ensure the effectiveness and integrity of the data.The design idea of the PH verification framework is based on the following principles:
1. Definition of verification rules: The PH verification framework allows developers to define various verification rules to verify different types of data according to specific needs.These verification rules can be applied to the field alone, or can be combined together to form complex verification conditions.
2. Flexibility of the verification device: The PH verification framework provides a variety of built -in verifications, such as compulsory fields, length, regular expression, etc.At the same time, it also allows developers to customize verifications to meet specific verification requirements.By customized verification, developers can easily expand the function of the framework to meet various customized verification needs.
3. Customization of error messages: The PH verification framework allows developers to customize the error message output when the customization failed.Developers can customize the formats and contents of the error message according to the needs of the application in order to better show the reason for the user to verify the failure.
The following is an example that shows how to use the PH verification framework to verify a user's registration form:
import com.github.phzhou76.phvalidation.PHValidation;
import com.github.phzhou76.phvalidation.validator.*;
public class UserRegistrationForm {
private String username;
private String password;
public UserRegistrationForm(String username, String password) {
this.username = username;
this.password = password;
}
public boolean validate() {
PHValidation validation = new PHValidation();
validation.addField("username", username)
.addvalidator (New Notemptyvalidator ("Username cannot be empty")))))
.Addvalidator (New Lengthvalidator (6, 20, "user name length must be between 6 and 20 characters"););
validation.addField("password", password)
.addvalidator (New NoteMPTyValidator ("Password cannot be empty"))))))
.advalidator (New Lengthvalidator (8, 20, "password length must be between 8 and 20 characters"))))))))))))))
.addValidator(new RegexValidator("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$",
"The password must contain at least one lowercase letter, a capital letter, and a number"););
return validation.validate();
}
}
public class Main {
public static void main(String[] args) {
UserRegistrationForm form = new UserRegistrationForm("username", "password");
if (form.validate()) {
System.out.println ("Registry Author verification passes");
} else {
System.out.println ("Registration Evaluted Failure");
}
}
}
In the above example, the UserRegistrationForm class represents the user's registry, which contains a username and a password.By calling the value () method, you can use the PH verification framework to verify the form.First of all, we add a field to the framework through the value of value.addfield () to the framework, and add the corresponding verification device to each field.For example, for the user of the user, we added NoteMPTYVALIDATOR to verify that it cannot be empty and add a lengthvalidator to verify whether the length is within the specified range.Finally, the verification is executed by calling the value.Validate () method and returned the verification results.
When the verification fails, the framework will return FALSE and print the error message to the console.According to the defined error message, the reason for the specific verification failure can be output.When the verification is successful, the framework will return True, indicating that the registry verification is passed.
By using the PH verification framework, developers can easily verify the data entered by the user to improve the security and stability of the application.At the same time, the flexibility and customization of the framework also allows developers to customize verification logic according to specific needs to meet the individual verification requirements of the application.