@NotNull(message = "Mobile number cannot be null.")
@Max(value = 10, message ="Mobile number length should be 10 characters." )
@Min(value = 10, message ="Mobile number length should be 10 characters." )
@Column(name = "mob_no")
private Long mobNo;
it should not give the message "Mobile number length should be 10 characters." in case giving 10 Digits value
According to your description, you need a String
instead a Long
for mobNo
. Imagine you have a phone number like this: 0005550001
, as a Long
the value is 550001
. The validation for a String
should be something like:
@Size(min = 10, max = 10, message = "Mobile number length should be 10 characters.")
private String mobNo;
Edit: A good and more comprehensive alternative is to validate against a regular expression matching 10 digits:
@Pattern(regexp = "^\d{10}$", message = "Mobile number length should be 10 characters.")
private String mobNo;
Edit: Adding comment to the answer
If mobNo
is a Long
and you can't change it to String
, validate it with:
@Min(1_000_000_000L)
@Max(9_999_999_999L)
Long mobNo
This is the value range where a Long has 10 digits.
However, consider the example above before choosing Long or a phone number field:
Ex:0_005_550_001
,
this is a valid phone number, but as a Long
its represented like 550001
.