2

I'm validating the Edittext with regex by allowing particular characters in that i need to allow all the special characters to enter in edit text.

for allowing alpha and numbers im using the code

edittext.setFilters(new InputFilter[] { new PartialRegexInputFilter(
                    "[a-zA-z0-9]+") });

Like this i need to allow all special characters...

And also i try to give like this

edittext.setFilters(
new InputFilter[] 
    { new PartialRegexInputFilter(
        "[A-Za-z0-9!#$%&(){|}~:;<=>?@*+,./^_`-\'\" \t\r\n\f]+") 
    }
);` . 

But this give error for single and double quote characters...

6
  • define "special character" ? Commented Dec 26, 2012 at 11:36
  • so what your question is ? Commented Dec 26, 2012 at 11:37
  • 2
    You know how to do it for alphanumeric characters. Now just negate that to get all other characters. If this does not works, then add a definition of Special Characters to your question. Commented Dec 26, 2012 at 11:38
  • it will change according to user define in front end... Sometime they will define only to type number with special characters.. Commented Dec 26, 2012 at 11:40
  • And also i try to give like this [A-Za-z0-9!#$%&(){|}~:;<=>?@*+,./^_`-\'\" \t\r\n\f]. But this give error for single and double quote characters... Commented Dec 26, 2012 at 11:42

3 Answers 3

2

Is there anything you want to disallow? It sounds like you're trying to allow all alphanumeric character and allow all non-alphanumeric (i.e., special) characters.

The following regex will match all special characters:

[^A-Za-z0-9]
Sign up to request clarification or add additional context in comments.

Comments

2

Instead of adding every special character, use .*. This regex matches everything.

If you want to remove some characters from it, use [^a!c] alone, without .*, to match everything except a, ! and c.

Comments

1

I think the problem with your second pattern is the hyphen (-) in the middle of your character set. Move it to the end:

[A-Za-z0-9!#$%&(){|}~:;<=>?@*+,./^_`\'\" \t\r\n\f-]+

Hyphens cannot be in the middle because they specify ranges of characters [a-z] specifies all lowercase letters between a through z.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.