How can I check the USSD code entered in edittext before submitting
Here I put some code, kindly helping me.
ussd_edittext.addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
ussd_edittext.setError("Invalid password");
ussd_ok.setEnabled(false);
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (ussd_edittext.getText().toString().trim().replaceAll("[^0-9#*]", "").length() < 4) {
ussd_edittext.setError("USSD code must contain * and #");
ussd_ok.setEnabled(false);
}
}
public void afterTextChanged(Editable s) {
Character cr = s.toString().charAt(0);
if(s.length() < 1)
{
if(!( (cr == '*') ) )
{ ussd_edittext.setError("USSD code must contain * and #");
ussd_ok.setEnabled(false);
}
else {
ussd_ok.setEnabled(true);
}
}
else
{
String lc = s.toString().substring(0, ussd_edittext.length() - 1);
if(!( (cr == '*') && lc.equals("#") ) )
{
ussd_edittext.setError("USSD code must contain * and #");
ussd_ok.setEnabled(false);
}
else {
ussd_ok.setEnabled(true);
}
}
}
});
I want to check how it should contain * and #, and also 5 digits are minimal.
source
share