EdiText limits input character types [regular rules]

Android implements limiting input character types in EditText:

For example: only numbers, letters, and Chinese characters are allowed to be entered.

Go directly to the code:

1. Set up monitoring:

editText.addTextChangedListener(mTextWatch);

2. Process check characters during monitoring:

    private TextWatcher mTextWatch = 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){
            String editable = editText.getText().toString();
            String str = stringFilter(editable.toString());
            if(!editable.equals(str)){
                editText.setText(str);
                //Set the new cursor position
                mEditGroupName.setSelection(str.length());
            }
        }
 
        @Override
        public void afterTextChanged(Editable s) {
 
        }
    };

3. Regular verification code:

 public static String stringFilter(String str) throws PatternSyntaxException {
        //Only letters, numbers and Chinese characters are allowed
        String regEx = "[^a-zA-Z0-9\\一-\\龥]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

Expand knowledge points:

A series of regular checks

Regular expressions are used to match specific types of characters in a piece of text. The following is a summary of commonly used regular matches.

1. Match Chinese:

[\\一-\\龥]

2. English letters:

[a-zA-Z]

3. Numbers:

[0-9]

4. Match Chinese, English letters, numbers and underscores:

^[\\一-\\龥_a-zA-Z0-9] + $

Match Chinese, English letters, numbers and underscores, and determine the input length:

[\\一-\\龥_a-zA-Z0-9_]{4,10}

5. Cannot start with _

(?!_)

Cannot end with _

 (?!.*?_$) 

?

At least one Chinese character, number, letter, or underscore

[a-zA-Z0-9_\\一-\\龥] + $

Matches the end of the string

6. It only contains Chinese characters, numbers, letters, and underscores. The position of the underscore is not limited:

^[a-zA-Z0-9_\\一-\\龥] + $

7. A string consisting of numbers, 26 English letters or underscores

^\w + $

8. 2~4 Chinese characters

^[\\一-\\龥]{2,4}$

9. The longest regular expression must not exceed 7 Chinese characters, or 14 bytes (numbers, letters and underscores)

^[\\一-\\龥]{1,7}$|^[\dA-Za-z_]{1,14}$

10. Match double-byte characters (including Chinese characters):

[^x00-xff]

Comment: Can be used to calculate the length of a string (a double-byte character counts as 2, and an ASCII character counts as 1)

11. Regular expression to match blank lines:

ns*r

Comment: Can be used to delete blank lines

12. Regular expression to match HTML tags:

<(S*?)[^>]*>.*?|<.*? />

Comment: The version circulating on the Internet is too bad. The above one can only match part of it, and it is still powerless for complex nested tags.

13. Regular expression to match leading and trailing whitespace characters:

^s*|s*$

Comment: It can be used to delete whitespace characters (including spaces, tabs, form feeds, etc.) at the beginning and end of the line. It is a very useful expression.

14. Regular expression matching email address:

^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.- ]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$

Comment: Very useful for form validation

15. Mobile phone number:

^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9])) \d{8}$

16. ID card:

(^\d{15}$)|(^\d{17}([0-9]|X|x)$)

17. Regular expression to match URL:

[a-zA-z] + ://[^s]*

Comment: The version circulating on the Internet has very limited functions. The above one can basically meet the needs.

18. Is the matching account legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed):

^[a-zA-Z][a-zA-Z0-9_]{4,15}$

Comment: Very useful for form validation

19. Match domestic phone numbers:

d{3}-d{8}|d{4}-d{7}

Comment: Matching format such as 0511-4405222 or 021-87888822

20. Match Tencent QQ account:

[1-9][0-9]{4,}

Comment: Tencent QQ account starts from 10000

21. Match Chinese postal code:

[1-9]d{5}(?!d)

Comment: China’s postal code is a 6-digit number

22. Match ID card:

d{15}|d{18}

Comment: China’s ID card has 15 or 18 digits

23. Match ip address:

d + .d + .d + .d + 

Comment: Useful when extracting IP address

24. Match specific numbers:

? // Match positive integers

^[1-9]d*$

//Match negative integers

^-[1-9]d*$

//Match integers

^-?[1-9]d*$

// Match non-negative integers (positive integers + 0)

^[1-9]d*|0$

? // Match non-positive integers (negative integers + 0)

^-[1-9]d*|0$

//Match positive floating point numbers

^[1-9]d*.d*|0.d*[1-9]d*$

//Match negative floating point numbers

^-([1-9]d*.d*|0.d*[1-9]d*)$

//Match floating point numbers

^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0 + |0)$

//Match non-negative floating point numbers (positive floating point numbers + 0)

^[1-9]d*.d*|0.d*[1-9]d*|0?.0 + |0$

//Match non-positive floating point numbers (negative floating point numbers + 0)

^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0 + |0$

Comment: Useful when processing large amounts of data, please pay attention to corrections when applying it.

25. Match specific strings:

//Match a string consisting of 26 English letters

^[A-Za-z] + $

//Match a string consisting of 26 uppercase English letters

^[a-z] + $

//Match a string consisting of 26 lowercase English letters

^[a-z] + $

//Match a string consisting of numbers and 26 English letters

^[A-Za-z0-9] + $

//Match a string consisting of numbers, 26 English letters or underscores

^w + $

26.

The verification function and its verification expression when using RegularExpressionValidator to verify the control are introduced as follows:

Only numbers can be entered:

^[0-9]*$

Only n-digit numbers can be entered:

^d{n}$

Only at least n digits can be entered:

^d{n,}$

Only m-n numbers can be entered:

^d{m,n}$

Only numbers starting with zero and non-zero can be entered:

^(0|[1-9][0-9]*)$

Only positive real numbers with two decimal places can be entered:

^[0-9] + (.[0-9]{2})?$

Only positive real numbers with 1-3 decimal places can be entered:

?
^[0-9] + (.[0-9]{1,3})?$

?

Only non-zero positive integers can be entered:

?
^ + ?[1-9][0-9]*$

?

Only non-zero negative integers can be entered:

^-[1-9][0-9]*$

Only characters of length 3 can be entered:

^.{3}$

Only strings consisting of 26 English letters can be entered:

^[A-Za-z] + $

Only strings consisting of 26 uppercase English letters can be entered:

^[a-z] + $

Only a string consisting of 26 lowercase English letters can be entered:

^[a-z] + $

Only strings consisting of numbers and 26 English letters can be entered:

^[A-Za-z0-9] + $

You can only enter a string consisting of numbers, 26 English letters, or underscores:

^w + $

Verify user password: The correct format is: starting with a letter, length between 6-18,

^[a-zA-Z]w{5,17}$

Can only contain characters, numbers, and underscores.

Verify whether it contains characters such as ^% & amp;’,;=?$”: “[^% & amp;’,;=?$x22] + “

Only Chinese characters can be entered:

^[u4e00-u9fa5],{0,}$

Verify email address:

^w + [- + .]w + )*@w + ([-.]w + )*.w + ([-.]w + )*$

Verify InternetURL:

^http://([w-] + .) + [w-] + (/[w-./?% & amp;=]*)?$

Verify ID number (15 or 18 digits):

^d{15}|d{}18$

Verify the 12 months of a year: the correct format is: “01”-“09” and “1” “12”

^(0?[1-9]|1[0-2])$

Verify the 31 days of a month: the correct format is: “01” “09” and “1” “31”

^((0?[1-9])|((1|2)[0-9])|30|31)$

Regular expression to match Chinese characters:

[u4e00-u9fa5]

Match double-byte characters (including Chinese characters):

[^x00-xff]

Regular expression to match empty lines:

n[s| ]*r

Regular expression to match HTML tags:

/<(.*)>.*|<(.*) />/

Regular expression to match leading and trailing spaces:

(^s*)|(s*$)

Regular expression to match email addresses:

w + ([- + .]w + )*@w + ([-.]w + )*.w + ([-.]w + )*

Regular expression to match URL:

http://([w-] + .) + [w-] + (/[w- ./?% & amp;=]*)?