In modern web application development, data verification is a very important link. Whether it is user registration, login, or other scenarios involving user input, ensuring the legality and correctness of the data is essential. As one of the common user input items, the format verification of mobile phone numbers is particularly important. This article will introduce in detail how to use annotations in Java to verify the mobile phone number format, and combine code examples to help readers understand in-depth.
1. Introduction
During the development process, the data entered by users often need to be strictly verified to ensure that it complies with business rules. As a common user input item, mobile phone numbers usually need to meet the following requirements:
- Can't be empty
- Must comply with a specific format (for example, the mobile phone number in mainland China is usually 11 digits and starts with 1)
To implement these verification rules, Java provides a powerful data verification framework, which can easily be implemented with annotations. This article will focus on how to use annotations to verify the format of your mobile phone number.
2. The importance of data verification
Data verification is an important means to ensure system robustness and security. The following are several important functions of data verification:
- Prevent illegal data from entering the system: For example, incorrect mobile phone number format may cause SMS sending failure.
- Improve user experience: Pass the dual verification of the front-end and back-end, promptly prompt users for input errors.
- Ensure data consistency: Ensure that the data in the database complies with business rules and avoid dirty data.
In Java, data verification is usually implemented through annotation, which is simple, efficient and easy to maintain.
3. Data verification framework in Java
Data verification in Java mainly depends on packages (also known as Bean Validation). This package provides a series of annotations for verifying fields in Java objects. Commonly used annotations include:
- @NotBlank: The check string cannot be empty.
- @NotNull: The check field cannot be null.
- @Size: Check the length of the string or the size of the collection.
- @Pattern: Verify string format by regular expression.
These annotations can be used in combination with frameworks such as Spring Boot to achieve automated data verification.
4. Use annotations to verify the mobile phone number format
4.1 @NotBlank Annotation
@NotBlank annotation is used to verify that the string field cannot be empty. It is usually used for verification of required fields. For example:
@NotBlank(message = "The user's mobile phone number cannot be empty") private String userPhone;
If the userPhone field is empty or contains only spaces, the check will fail and the specified error message will be returned.
4.2 @Pattern Annotation
@Pattern annotation is used to check string format by regular expressions. For verification of mobile phone number format, @Pattern is the most commonly used annotation. For example:
@Pattern(regexp = "^1[3-9]\\d{9}$", message = "Mobile phone number format is incorrect") private String userPhone;
The regular expression ^1[3-9]\\d{9}$ here means:
- ^1: The mobile phone number must start with 1.
- [3-9]: The second digit must be a number between 3 and 9.
- \\d{9}$: followed by 9 digits.
4.3 The role of regular expressions
Regular expressions are powerful tools for checking string formats. Here are some common regular expression rules:
- ^: Match the beginning of the string.
- $: Match the end of the string.
- \\d: Match numeric characters.
- {n}: Match the previous character exactly n times.
By combining these rules, complex verification logic can be constructed.
5. Complete code example
Here is a complete Java class example showing how to use annotations to verify the mobile phone number format:
import ; import ; import ; @Data public class UserVerifyReq implements Serializable { private static final long serialVersionUID = 6032064528363065061L; @NotBlank(message = "The user's mobile phone number cannot be empty") @Pattern(regexp = "^1[3-9]\\d{9}$", message = "Mobile phone number format is incorrect") @ApiModelProperty(required = true, value = "User Mobile Number") private String userPhone; @ApiModelProperty(value = "Agent ID") private Long agentId; @ApiModelProperty(value = "Whether to bind a mobile phone number") private Integer isBound; }
Code parsing
@NotBlank: Make sure the userPhone field is not empty.
@Pattern: Verify the phone number format through regular expressions.
@ApiModelProperty: Used to generate API documents, indicating the purpose of the fields and whether they are required.
6. FAQs and Solutions
6.1 Regular expression mismatch
If the regular expression is written incorrectly, it may cause the verification to fail. It is recommended to use an online regular expression testing tool (such as) for debugging.
6.2 Verification annotation is not effective
If the verification annotation does not take effect, the following reasons may be:
- No dependencies were introduced.
- Data verification is not enabled in Spring Boot.
6.3 International support
If you need to support multilingual error prompts, you can configure the international resource file through MessageSource.
7. Summary
This article introduces in detail how to use annotations in Java to verify the mobile phone number format. Through @NotBlank and @Pattern annotations, non-empty checksum format verification of mobile phone numbers can be easily achieved. Combined with regular expressions, it can meet various complex verification needs.
Data verification is a link that cannot be ignored in the development process. The rational use of annotations and regular expressions can significantly improve the maintainability of the code and the robustness of the system. I hope that the content of this article can help readers better understand and apply data verification technology in Java.
Appendix: Commonly used regular expressions
- Mobile phone number: ^1[3-9]\\d{9}$
- Email: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$
- ID number: ^\\d{17}[\\dXx]$
This is the article about this detailed guide on using annotations to verify the format of mobile phone numbers in Java. For more relevant content on Java annotations to verify the format of mobile phone numbers, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!