Verify mobile number with Hibernate annotation

I have an object called User and I want to check the mobile number field

The mobile number field is optional, you can leave it blank, but it must be a ten-digit number.

If the user enters any value less than 10 digits in length, then an error should be thrown.

Below is my User class.

public class User {

    @Size(min=0,max=10)
    private String mobileNo;

}

When I used the @Size annotation, as mentioned above, I could check values ​​greater than 10, but if the user entered less than 10 digits, no error occurred.

My requirement: if the user left the mobileNo field empty, but if a value is entered, then the check should ensure that the entered number will consist of only 10 digits and 10 digits.

?

+5
2

@Size(min=10,max=10) , null.

@NotNull, null .

String, @Pattern validator:

@Pattern(regexp="(^$|[0-9]{10})")

, .

+18

, libphonenumber Google Code, .

+3

All Articles