Thread: Mobile number validator.

Forum : Problem Solving   25-9-2018 02:41:44 PM
User Image
satya1995

Newbie

Joined: Feb 21, 2018

Points: 100

Threads: 11

Posts: 23

Answer Icon Mobile number validator.

| Quote Date : Sep 25, 2018    Views:200    

Write a function that accepts a string, and returns true if it is in the form of a Mobile number.
Assume that any integer from 0-9 in any of the spots will produce a valid phone number.

Only worry about the following format:
+91-9876485984 (Indian Format)

for example :
validPhoneNumber("+91-7869485756")  => return true
validPhoneNumber("7869485756")  => return false
validPhoneNumber("+917869485756")  => return false
validPhoneNumber("+91-786948")  => return false
validPhoneNumber("+00-7869485756")  => return false

NOTE:- write a optimized code

Comments

User Image
satya1995

Newbie

Joined : Feb 21, 2018

Points : 100

Threads: 11

Posts: 23

Re: Mobile number validator.

Reply Date : Dec 14, 2018

Your answer is good, it is an optimized solution.

public class PhoneNumber {
    public boolean validPhoneNumber(String mobileNo) {
        return (mobileNo.startsWith("+91-")&& mobileNo.trim().length()==14);
    }
   
    public static void main(String[] args) {
        PhoneNumber phoneNumber = new PhoneNumber();
        System.out.println(phoneNumber.validPhoneNumber("+91-9876543210"));
        System.out.println(phoneNumber.validPhoneNumber("9876543201"));
    }
}

Replied by Dharam405
User Image
Dharam405

Newbie

Joined : Oct 26, 2018

Points : 100

Threads: 2

Posts: 10

Re: Mobile number validator.

Reply Date : Dec 12, 2018

public class PhoneNumber {
    public boolean validPhoneNumber(String mobileNo) {
        return (mobileNo.startsWith("+91-")&& mobileNo.trim().length()==14);
    }
   
    public static void main(String[] args) {
        PhoneNumber phoneNumber = new PhoneNumber();
        System.out.println(phoneNumber.validPhoneNumber("+91-9876543210"));
        System.out.println(phoneNumber.validPhoneNumber("9876543201"));
    }
}

User Image
Dharam405

Newbie

Joined : Oct 26, 2018

Points : 100

Threads: 2

Posts: 10

Re: Mobile number validator.

Reply Date : Dec 12, 2018

public class PhoneNumber {
    public boolean validPhoneNumber(String mobileNo) {
        return (mobileNo.startsWith("+91-")&& mobileNo.trim().length()==14);
    }
   
    public static void main(String[] args) {
        PhoneNumber phoneNumber = new PhoneNumber();
        System.out.println(phoneNumber.validPhoneNumber("+91-9876543210"));
        System.out.println(phoneNumber.validPhoneNumber("9876543201"));
    }
}

User Image
jagadish4

Newbie

Joined : Oct 14, 2018

Points : 105

Threads: 0

Posts: 9

Answer IconRe: Mobile number validator.

Reply Date : Dec 5, 2018

[

public class ValidateMobile {
public static boolean isValidMobileNumber(String str) {
try {
if (str.length() == 14 && str.contains("-") && str.startsWith("+") && str.split("-")[0].equals("+91")) {
Long.parseLong(str.split("-")[1]);
return true;
}
} catch (NumberFormatException e) {
return false;
}
return false;
}
}

Load More

No More Comments