C
CodeGrommet
I'm trying to validate a phone number value. I have passed the value
to my constructor as a String type because I would like to catch the
leading zero in the number. I want to iterate through the String
phoneNum and confirm that only [0-9] were entered as characters of
phoneNum. Below is my code attempt which fails:
phoneNum = phoneNum.trim();
Pattern p = Pattern.compile("\\d");
boolean found = p.matcher(phoneNum).lookingAt();
if (!found){
System.out.println("Error in contact phone number");
}
else{
this.phoneNum = phoneNum;
System.out.println("Phone number was successfully
assigned");
}
Could anyone post code/ideas/principles that could make this idea
work, please?
Thanks,
Rick
to my constructor as a String type because I would like to catch the
leading zero in the number. I want to iterate through the String
phoneNum and confirm that only [0-9] were entered as characters of
phoneNum. Below is my code attempt which fails:
phoneNum = phoneNum.trim();
Pattern p = Pattern.compile("\\d");
boolean found = p.matcher(phoneNum).lookingAt();
if (!found){
System.out.println("Error in contact phone number");
}
else{
this.phoneNum = phoneNum;
System.out.println("Phone number was successfully
assigned");
}
Could anyone post code/ideas/principles that could make this idea
work, please?
Thanks,
Rick