Last active
November 18, 2019 09:55
-
-
Save shohiebsense/aa316ffbba387256a5e7af24e3cbbadf to your computer and use it in GitHub Desktop.
It is essential to handle leading zeros. https://stackoverflow.com/questions/33229792/regex-input-filter-for-android-to-prevent-more-than-one-leading-zero.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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