Valid Username Regular Expression

Sort by

recency

|

478 Discussions

|

  • + 0 comments

    public static final String regularExpression = "^[a-zA-Z][a-zA-Z0-9_]{7,29}$";

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner input = new Scanner(System.in);
       int count = input.nextInt();
        input.nextLine();
    
        for(int i=0;i<count;i++){
        String username = input.nextLine();
    
    
        if(checkUsername(username)){
            System.out.println("Valid");
        }
        else{
            System.out.println("Invalid");
        }
        }
    
        input.close();
    
    }
    public static boolean checkUsername(String username){
        int length = username.length();
    
        if(length<8 || length>30){
            return false;
        }
        if(!Character.isLetter(username.charAt(0))){
            return false;
        }
    
        for(int i=1;i<length;i++){
           char ch = username.charAt(i);
           if(!Character.isLetterOrDigit(ch) && ch!='_'){
            return false;
           }
        }
        return true;
    }
    

    }

    
    
  • + 0 comments

    JAVA SOLUTION

    import java.io.; import java.util.;

    public class Solution {

    public static void validator(String word){
    
        // First character must be a letter : ^[a-zA-Z]
    
        // Following characters can be either letters , numbers or _ : [a-zA-Z_0-9]
    
        // Must have 8-30 characters so after the first validation to the last character 
        // can have 7 to 29 =  {7,29}$
    
        if (word.matches("^[a-zA-Z][a-zA-Z_0-9]{7,29}$")) {
            System.out.println("Valid");
        }
        else{
           System.out.println("Invalid"); 
        }
    
    }
    
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
        Scanner sc = new Scanner(System.in);
        int numWords = sc.nextInt();
    
        sc.nextLine();
    
        for (int i = 0; i < numWords; i++) {
            String word = sc.nextLine();
            validator(word);
    
        }
    
        sc.close();
    
    
    }
    

    }

  • + 0 comments

    public static final String regularExpression = "^[a-zA-Z][a-zA-Z0-9_]{7,29}$";

  • + 0 comments

    TestCase for Julia is wrong