Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Digits Validation [closed]

I'm using validation in a int attribute:

@Digits(integer=6,fraction=0)
private Integer idOrder;

But I wanted this attribute has exactly 6 digits.

How can I do that? (Using JSP+Spring+Hibernate)

like image 683
Renato Shumi Avatar asked Mar 09 '26 14:03

Renato Shumi


1 Answers

As you need the 6 digits, I think you should use a String to the property. If in some situations you need it as int, you make a

 public int getIdOrder(){ return Integer.parseInt(idOrder);}

On the String you can use pattern validation. It would be somethind like this:

@Pattern(regexp="\\d{6}")
private String idOrder;

Another option, is keep the idOrder a int and make a getter for the string:

public String getIdOrgetAsString(){
    String s = "000000" + idOrder;
    return s.substring(s.length() - 6);
}

Anyway, there are some options.

like image 61
digao_mb Avatar answered Mar 11 '26 04:03

digao_mb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!