Thread: Mobile number validator.

Forum : Problem Solving   25 Sep, 2018 14:41
User Image
satya1995

Newbie

Joined: 21 Feb, 2018

Points: 110

Threads: 11

Posts: 34

Answer Icon Mobile number validator.

| Quote Date : 25 Sep, 2018    Views:281    

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 : 21 Feb, 2018

Points : 110

Threads: 11

Posts: 34

Re: Mobile number validator.

Reply Date : 14 Dec, 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 : 26 Oct, 2018

Points : 100

Threads: 3

Posts: 11

Re: Mobile number validator.

Reply Date : 12 Dec, 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 : 26 Oct, 2018

Points : 100

Threads: 3

Posts: 11

Re: Mobile number validator.

Reply Date : 12 Dec, 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 : 14 Oct, 2018

Points : 105

Threads: 1

Posts: 9

Answer IconRe: Mobile number validator.

Reply Date : 5 Dec, 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