Total Pageviews

2019/02/07

[Java] How to do IP validation in Java?

Problem
How to do IP validation in Java?

How-To
Here has sample code:
    import lombok.Data;
    import javax.validation.constraints.NotNull;
    import javax.validation.constraints.Pattern;

    @Data
    public class Param {
        
        @NotNull(message = "請提供性別")
        private String gender;
        
        @NotNull(message = "請提供呼叫者 IP address")
        @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])$", 
                 message = "呼叫者 IP address 不合法")
        private String srcIp;
    }

Reference
[1] https://stackoverflow.com/a/14877281/6314840

No comments: