1

I've a text field on UI. It does not restrict user to enter any special char. But while storing it in DB I want anything to be removed except alphabets and numerics.

It can be done at the client (JavaScript) side or the server (Java) side (though I want to do it on server side).

I used

str.replaceAll("[^a-zA-Z0-9]", "");

to do the task, but it would give an error:

Invalid char sequence

when entered something like "24 \ 7". Is there any way to handle these kind of escape sequence or I should do it on client side(using javascript)? means remove all unwanted chars in js only?

2
  • "24 \ 7" is you string input and you want to convert this into "24 7". Am I correct? Commented Apr 29, 2013 at 17:45
  • This sounds like more of an issue of how str is created than the replace. Can you show that code? Commented Apr 29, 2013 at 17:51

1 Answer 1

3

Backslash literal characters need to occur in pairs otherwise Java will expect a carriage control character (such as \t or \n)

String str = "24 \\ 7";
                  ^
Sign up to request clarification or add additional context in comments.

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.