【问题标题】:How to check the value entered in EditText is aplhanumeric or not?如何检查 EditText 中输入的值是否为字母数字?
【发布时间】:2011-02-09 09:34:36
【问题描述】:

我的应用程序将用户的用户 ID 作为输入,用户 ID 是字母数字,即只有第一个字符是 (a-z),其他部分是数字。如何验证这种类型的输入(如 G34555)?

【问题讨论】:

    标签: android validation input android-edittext


    【解决方案1】:

    使用正则表达式。这应该假设第一个字母可以是大写或小写:

     Pattern p = Pattern.compile("[a-zA-Z][0-9]+");
    
     Matcher m = p.matcher("some text you want");
     boolean isAlphaNum = m.matches();
    

    【讨论】:

      【解决方案2】:

      我已经通过使用简单的string 函数matches 解决了问题

      String str="mystring";

      str.matches("[a-zA-Z][0-9]+");

      【讨论】:

        【解决方案3】:

        http://osdir.com/ml/Android-Developers/2009-11/msg02501.html 似乎是一个更体面的解决方案,它不允许输入不被接受的字符。

        链接代码:

        InputFilter filter = new InputFilter() {
            public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {
                for (int i = start; i < end; i++) {
                    if (!Character.isLetterOrDigit(source.charAt(i))) {
                        return "";
                    }
                }
                return null;
            }
        };
        
        edit.setFilters(new InputFilter[]{filter});
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-07-25
          • 2017-07-21
          • 1970-01-01
          • 2014-07-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-22
          相关资源
          最近更新 更多