【问题标题】:How to validate ussd code for balance checking如何验证用于余额检查的 ussd 代码
【发布时间】:2017-03-23 01:32:22
【问题描述】:

发送前如何验证在edittext中输入的USSD代码

这里我放了一些代码,请帮助我。

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);
                    }
                }
            }
        });

我想验证它必须包含 * 和 # 也是最少 5 位数字。

【问题讨论】:

    标签: android validation android-edittext


    【解决方案1】:

    我曾经做过这样的事情。

    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().length() < 4 && ( ! t.contains(""+ussd_edittext))) {
                            ussd_edittext.setError("USSD code must contain * and #");
                            ussd_ok.setEnabled(false);
                        }
                    }
    
                    @Override
                    public void afterTextChanged(Editable s)
                    {
                        if (ussd_edittext.getText().toString().trim().length() > 4)
                        {
                            Character fc = ussd_edittext.getText().toString().charAt(0);    
                            String lc = ussd_edittext.getText().toString().substring(ussd_edittext.length() - 1);
                            if(!((fc == '*') && lc.equals("#")))
                            {
                                ussd_edittext.setError("USSD code must contain * and #");
                                ussd_edittext.requestFocus();
                            }
                            else
                            {                      ussd_ok.setEnabled(true);               }
                        }
                        else
                        {                        ussd_edittext.setError("USSD code must contain * and #");                 }
                    }
                });
    

    【讨论】:

      【解决方案2】:

      这可能会有所帮助:

      if (Pattern.matches("(\\*[0-9]+[\\*[0-9]+]*#)", ussd_edittext.getText().toString()) && ussd_edittext.length() >= 5) {
              //USSD code is valid
          } else {
              //USSD code is not valid
          }
      

      【讨论】:

        猜你喜欢
        • 2015-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-31
        • 2017-02-18
        相关资源
        最近更新 更多