Skip to content

Instantly share code, notes, and snippets.

@anymaniax
Created December 16, 2016 10:30
Show Gist options
  • Save anymaniax/49ea7a39d84867a1f1bc6605fbf80c7b to your computer and use it in GitHub Desktop.
Save anymaniax/49ea7a39d84867a1f1bc6605fbf80c7b to your computer and use it in GitHub Desktop.
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > start) {
String destTxt = dest.toString();
String resultingTxt = destTxt.substring(0, dstart) + source.subSequence(start, end) + destTxt.substring(dend);
if (!resultingTxt.matches("^\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3}(\\.(\\d{1,3})?)?)?)?)?)?")) {
return "";
} else {
String[] splits = resultingTxt.split("\\.");
for (int i = 0; i < splits.length; i++) {
if (Integer.valueOf(splits[i]) > 255) {
return "";
}
}
}
}
return null;
}
};
et_setting_ip.setFilters(filters);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment