Skip to content

Instantly share code, notes, and snippets.

@shohiebsense
Last active November 18, 2019 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shohiebsense/aa316ffbba387256a5e7af24e3cbbadf to your computer and use it in GitHub Desktop.
Save shohiebsense/aa316ffbba387256a5e7af24e3cbbadf to your computer and use it in GitHub Desktop.
public class IntegerInputFilter implements InputFilter {
private Pattern pattern = Pattern.compile("^(?!0{1,})\\d+(?:\\.\\d+)?$");
//or ..{1,}..
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//Log.e("word "+source+ " "+dest);
Matcher matcher = pattern.matcher(dest.toString()+source.toString());
//you can handle with length == 1 return null;
if(!matcher.matches()){
Log.e("leading zero");
return "";
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment