开发者

How to get the Username only the characters not the whitespaces?

In jsp applic开发者_运维技巧ation, i am checking username and password, when entering whitespace in username means it didnt show the error, how to validate it?


Maybe you want to trim() the value before checking the username/password:

String password = getPassword().trim();

This will remove all leading and trailing whitespaces.


Why do you do it in the back end, you can use a javascrit regex :

^([a-zA-Z0-9.]+@){0,1}([a-zA-Z0-9.])+$

This will permit A-Z, a-z, 0-9 and ., and at most one @


Do a regex validation, if your username contains only digits and alphabets, do something like

[A-Za-z0-9]+

Change it based on length limitations


You have two different questions:

  1. How to get the Username only the characters not the whitespaces?

    To do that, you can simply replace all the whitespaces in your string:

    String name = " blah is a word";
    name = name.replace(" ", "");
    
  2. How to validate the username?

    To do that, you can simply check if the string contains a whitespace.

    String name = " blah is a word";
    if (name.contains(" "))
    {
        // Wrong
    } else
    {
        // Correct
    }
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜