1

I'm creating a form that the user must input name without contain number or other character.

Part of the code is:

bSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String tampSalesName = salesName.getText().toString();
                String lala = cekHuruf(tampSalesName);
                if(lala.equals("number")){
                    Toast.makeText(RegisterSales.this, "Name must letters", Toast.LENGTH_SHORT).show();
                }


    public String cekHuruf(String a){
        String zzzz="";
        String aaa="";
        int ab=0;
        for (int i = 0; i < a.length(); i++) {
            int abc = i++;
            aaa= a.substring(i, abc);
            try {
                ab = Integer.parseInt(aaa);
            } catch (NumberFormatException sdef) {
                // TODO: handle exception
                zzzz="number";
            }       
        }
        return zzzz;
    }

And it will open String.class and will show error:

private StringIndexOutOfBoundsException startEndAndLength(int start, int end) {
        throw new StringIndexOutOfBoundsException(this, start, end - start);
    }

The logic : If the first word is letter it will always tamp letter. So what your suggestion about edittext must letter. I already try several way from change the input type in xml and try to use method in android EditText alphabet-only validation but still cannot work.

Community
  • 1
  • 1
novie
  • 37
  • 6

3 Answers3

2

Use this code

name.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            Is_Valid_Person_Name(name); // pass your EditText Obj here.
        }
    });

public void Is_Valid_Person_Name(EditText edt) throws NumberFormatException {
    if (edt.getText().toString().length() <= 0) {
        edt.setError("Accept Alphabets Only.");
        valid_name = null;
    } else if (!edt.getText().toString().matches("[a-zA-Z ]+")) {
        edt.setError("Accept Alphabets Only.");
        valid_name = null;
    } else {
        valid_name = edt.getText().toString();
    }

}

it will help.

Rahul
  • 479
  • 3
  • 18
0
            <EditText
                android:id="@+id/username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" >

            </EditText>
John
  • 8,846
  • 8
  • 50
  • 85
0

Use this code

        b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String a=salesName.getText().toString();
            Boolean z=cekHuruf(a);
            if(!z)
            {
                Toast.makeText(MainActivity.this, "Name must letters", Toast.LENGTH_SHORT).show();
            }
        }
        public boolean cekHuruf(String b){
            char[] chars = b.toCharArray();

            for (char c : chars) {
                if(!Character.isLetter(c)) {
                    return false;
                }
            }

            return true;
        }
    });
Karthikeyan
  • 1,119
  • 1
  • 15
  • 33