Friday, 22 March 2013

Restrict Edittext input content using Input Filters in ANDROID

InputFilter filter = new InputFilter() {
     public CharSequence filter(CharSequence source, int start, int end,
	    Spanned dest, int dstart, int dend) {
      // TODO Auto-generated method stub
	for (int i = start; i < end; i++) {
	    if (Character.isLetter(source.charAt(i))) {
		     System.out.println("Input consist of only characters from 'a' to 'z'");
		     return "";
		}
	}
	return null;
	} };
    edit.setFilters(new InputFilter[]{filter}); 
Here the filters will check whether the input content has only characters from 'a' to 'z'.
same way use the condition below to have characters from'a' to 'z' and '0' to '9' and avoid special characters.
Character.isLetterOrDigit(source.charAt(i))

1 comment:

  1. Hi,

    Is there a way to give user a visual feedback when he enter a wrong value ?

    Thx in advance,

    Simon

    ReplyDelete