javausernametoken

How to determine if a username is correct?


I need to determine if a username in the form of an email (something@something.com) is in the correct form. The necessary parameters for the username being correct are as follows:

  1. there is something before the @ symbol
  2. there is text between the @ symbol and the ".com"
  3. the username ends in .com

These are the only parameters, if they are met the method will return true and if they are all not met it will return false.


Solution

  • public static boolean isCorrectUsername(String username){
        var atPosition = username.indexOf("@");
        return atPosition > 0 && username.endsWith(".com") &&  username.length() > atPosition + 5;
    }