Monday 30 May 2016

Java program to check whether a given number is a valid mobile number or not.

import java.util.regex.*;
class RegexDemo6
{
   public static void main(String[] args)
    {
        Pattern p=Pattern.compile("(0/91)?[7-9][0-9]{9}");
        Matcher m=p.matcher(args[0]);
        if(m.find() && m.group().equals(args[0]))
        {
System.out.println("valid mobile number");
         }
        else
        {
System.out.println("invalid number");
         }
      }
   }

Output 

                         1st run : java RegexDemo6  9439202111
                         valid mobile number
                         2nd run : java RegexDemo6  929291929394
                         invalid number

No comments:

Post a Comment