使用edittext的IP地址验证器

时间:2018-12-02 14:57:29

标签: android regex validation android-edittext ip

我希望用户在edittext中输入IP地址并验证其是否有效。这是我的代码。如何验证?我也提到了此链接Validation in EditText allow IP or web Url host,但无法实现。请帮助

 private static final String IPADDRESS_PATTERN =
        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

public class MainActivity extends AppCompatActivity {
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
if (ip_addr.isEmpty() ||!ip_addr.matches(IPADDRESS_PATTERN)) {
                    builder.setTitle("Something went wrong!!");
                    displayAlert("You have not entered valid IP Address");

}
}
}

0 个答案:

没有答案