I'm looking for a possiblity to validate IP addresses in my Spring roo project.
My entity looks like this
package com.ip.test.domain;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class IP {
@NotNull
@Size(min = 7, max = 15)
private String ip;
@ManyToOne
private Hoster Hoster;
}
With this setup it validates only if the string contains 7 to 15 characters, but not really if it's an IP address.
Something like
@validIpAddress
private String ip;
would be nice.
Any idea if that's possible?
You can use the JSR 303 Pattern validator, with an IP address regex:
@NotNull
@Pattern(regexp = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$")
private String ip;
edit: escape backslash