Checking Mobile Number Validity using Java

import java.util.regex.*;

public class Main {
    public static boolean isValid(String s)
    {
        Pattern p=Pattern.compile("(0|\\+91[\\s])?[7-9][0-9]{9}");
        Matcher m=p.matcher(s);
        return (m.find() && m.group().equals(s));
    }
    
    public static void main(String args[]) {
       String str="+91 8412042118";
        if(isValid(str))
        {
            System.out.println("It is a valid mobile number");
        }
        else
        {
            System.out.println("It is invalid mobile number");
        }  
    }
}

Output

It is a valid mobile number

Comments