Patterns and Regular Expressions
In Java usually validate with some specific characters, we should use Regular Expressions.
Same way in Android, here we use Patterns.
More info about the Patterns: Click here
In android EditText, we can use custom input filter to perform this.
This is for maximum input digits before and after decimal pointer.
CustomInputFilter.Java
public class CustomInputFilter implements InputFilter {
int maxDigitsBeforeDecimalPoint;
int maxDigitsAfterDecimalPoint;
public CustomInputFilter(int maxDigitsBeforeDecimalPoint, int maxDigitsAfterDecimalPoint) {
this.maxDigitsBeforeDecimalPoint = maxDigitsBeforeDecimalPoint;
this.maxDigitsAfterDecimalPoint = maxDigitsAfterDecimalPoint;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"[-]?(([0-9]{1})([0-9]{0," + (maxDigitsBeforeDecimalPoint - 1) + "})?)?(\\.[0-9]{0," + maxDigitsAfterDecimalPoint + "})?"
)) {
if (source.length() == 0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
}
Apply in your activity like this,
editText.setFilters(new InputFilter[]{new CustomInputFilter(10, 2)});
For Validate Ip Address - (([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]) or Patterns.IP_ADDRESS
For Validate File Name - ^([a-zA-Z0-9-_ ]{2,30})\.(3gp|avi|mp4)+$
For not allow the special characters - \`|\~|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\+|\=|\[|\{|\]|\}|\||\\|\'|\<|\,|\.|\>|\?|\/|\""|\;|\:|\s
Validate Email Address = Patterns.EMAIL_ADDRESS;
Validate Phone Number = Patterns.PHONE;
Same way in Android, here we use Patterns.
More info about the Patterns: Click here
In android EditText, we can use custom input filter to perform this.
This is for maximum input digits before and after decimal pointer.
CustomInputFilter.Java
public class CustomInputFilter implements InputFilter {
int maxDigitsBeforeDecimalPoint;
int maxDigitsAfterDecimalPoint;
public CustomInputFilter(int maxDigitsBeforeDecimalPoint, int maxDigitsAfterDecimalPoint) {
this.maxDigitsBeforeDecimalPoint = maxDigitsBeforeDecimalPoint;
this.maxDigitsAfterDecimalPoint = maxDigitsAfterDecimalPoint;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
StringBuilder builder = new StringBuilder(dest);
builder.replace(dstart, dend, source
.subSequence(start, end).toString());
if (!builder.toString().matches(
"[-]?(([0-9]{1})([0-9]{0," + (maxDigitsBeforeDecimalPoint - 1) + "})?)?(\\.[0-9]{0," + maxDigitsAfterDecimalPoint + "})?"
)) {
if (source.length() == 0)
return dest.subSequence(dstart, dend);
return "";
}
return null;
}
}
Apply in your activity like this,
editText.setFilters(new InputFilter[]{new CustomInputFilter(10, 2)});
For Validate Ip Address - (([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]) or Patterns.IP_ADDRESS
For Validate File Name - ^([a-zA-Z0-9-_ ]{2,30})\.(3gp|avi|mp4)+$
For not allow the special characters - \`|\~|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\+|\=|\[|\{|\]|\}|\||\\|\'|\<|\,|\.|\>|\?|\/|\""|\;|\:|\s
Validate Email Address = Patterns.EMAIL_ADDRESS;
Validate Phone Number = Patterns.PHONE;
Comments