lundi 6 juillet 2020

Why Phone Number validate without regEx giving dead code in java

index increment showing dead code why so ?? before adding special character condition it was working fine no dead code was showing and i want to validate special character condition as well.

public class ValidatePhoneNumber {

void validatePhoneNumber(String pNumber) {

    // 1st Case - +91 9765463742 have to check for + sign
    boolean flag = false;
    String specialCharacter = "!@#$%^&*()-/`~:<>/?|=.,";
    if (pNumber.startsWith("+") && pNumber.length() == 14) {

        for (int index = 1; index < pNumber.length(); index++) {
            if ((Character.isDigit((pNumber.charAt(index))) || Character.isSpaceChar((pNumber.charAt(index))))
                        && (!(specialCharacter.contains(Character.toString(pNumber.charAt(index)))))
                            && (!(Character.isLetter(pNumber.charAt(index))))) {
        
                flag = true;
            }
             else
                System.out.println(pNumber.charAt(index) + " " + pNumber + " Number is Invalid");
            flag = false;
            break;
        }
        if (flag == true) {
            System.out.println("Number " + pNumber + " is Valid");
        }
    }


public static void main(String[] args) {
    ValidatePhoneNumber phoneNumber = new ValidatePhoneNumber();
    phoneNumber.validatePhoneNumber("+91 975644@742");
    phoneNumber.validatePhoneNumber("09765463742");

Aucun commentaire:

Enregistrer un commentaire