-1

Basically i am trying to restrict a user not to input characters that are not allowed in username box. i found that this could be implement by String.matches()

String UcharSet = "[a-zA-Z0-9-~!@#().]+";
boolean UMORN = "Username.is@example.com".matches(UcharSet);
if(UMORN != true)
    UNotAllowedCharEC = "0x00000030";

as you can see i have string of characters to be allowed in my username box but somehow when i input @ it return false although i have it in my allowed string list.

and do tell should i add any other characters to be allowed for my username box.

jacky
  • 199
  • 1
  • 4
  • 15
  • I tried it and `UMORN` is `true` for me. Regarding additional characters, you may want to include underscore `_` as well. – dty Sep 02 '16 at 07:29
  • i just need to assign `"0x00000030" to UNotAllowedCharEC` if the user input a restricted character, so am i doing the right way? – jacky Sep 02 '16 at 07:42
  • The code seems to be working fine. Just one advice, there is no point in comparing boolean variables with boolean literals in conditions. You can just write if( !UMORN). – uoyilmaz Sep 02 '16 at 07:47
  • **Don't** use regex to validate an email address. See [Using a regular expression to validate an email address](http://stackoverflow.com/q/201323/5221149) – Andreas Sep 02 '16 at 08:13

1 Answers1

0

I just tested this and '@' results in true. You problem probably lies elsewhere.

Valid

Community
  • 1
  • 1
Kelvin
  • 574
  • 3
  • 13
  • oh i get the point now, actually i had the username sent from a client browser, which if you input `@` it converts it to `%40` in the air which i didn't had the `%` in my allowed list, and i was getting the error. now can you tell me how to allow @ here in my condition – jacky Sep 02 '16 at 08:16
  • URLDecoder.decode(str, "utf-8"); This will turn the %40 into @ – Kelvin Sep 02 '16 at 08:20