Validate [Preference] filename from EditText using regex

我们两清 提交于 2021-01-29 14:11:57

问题


[FIRST APP] I am providing users with an option to have multiple profiles using different preference files. The users can name their profiles themselves. I have made a custom dialog box to take filename as input in edittext. A valid filename should contains only [A-Z, a-z, 0-9, _, -]. I am letting the user know about this restriction.

Whenever a user enters an invalid character, I want that character to be deleted from edittext and preferably display a toast. After a brief search I believe that this task can be performed easily using regular expressions (as compared to my naive string based approach using textwatcher on edittext). But, I have never worked with regex before and need help regarding this.

ps - A concerning usecase is when the user goes berserk and types random character in edittext.


回答1:


Here is the working code for above specific scenario, we attach a textwatcher to our edittext and then verify the input using a pattern (Thnaks to huidube) :

    profile_name.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) { }

        @Override
        public void afterTextChanged(Editable s) {
            String str = s.toString();
            if (!(str.matches("[a-zA-Z0-9-_ ]*"))) {
                str = removeIllegalChar(str).trim(); //trim whitespaces
                profile_name.setText(str);
                profile_name.setSelection(str.length());  //use only if u want to set cursor to end
            }
        }
    });

    private String removeIllegalChar(String str) {
    for (int i=0; i < str.length(); i++) {
        if (!(String.valueOf(str.charAt(i)).matches("[a-zA-Z0-9-_ ]*"))) {
            //as the callback is called for each character entered, we can return on first non-match
            //maybe show a short toast        
            return str.substring(0, i) + str.substring(i+1);
        }
    }
    return str;
}

I have two test devices, one a dying veteran and a Moto G and the above procedure works smoothly on both. So, I guess this is acceptably optimal.




回答2:


I would just check every letter for matching your regex each time an input is done.

I am using http://regexpal.com/

you would need something like:

public String validateInput(String input){
    String input = "yourStr1ng$";
    for (int i = 0; i < input.length(); i++) {
        if ((input .charAt(i) + "").matches("[1-9]||[a-z]")) {
            System.out.println(input.charAt(i) + " matching");
        } else {
            System.out.println(input.charAt(i) + " failed");
            //removeLetter(input charAt(i));
        }
    }
    return true;
}

returns:

y matching
o matching
u matching
r matching
S failed
t matching
r matching
1 matching
n matching
g matching
$ failed


来源:https://stackoverflow.com/questions/26434342/validate-preference-filename-from-edittext-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!