Need help with regex please

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
 
R

-Rick-

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 toiteratethrough 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

//****************** Solved! ****************************
//validate phone number input
phoneNum = phoneNum.trim();
Pattern p = Pattern.compile("[0-9]*");
boolean found = p.matcher(phoneNum).matches();
if (!found){
System.out.println("Error in contact phone number");
}
else{
this.phoneNum = phoneNum;
System.out.println("Phone number was successfully
assigned");
}

this.name = name;
 
J

Jeff Higgins

CodeGrommet said:
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?

using: telephone number format:
<http://en.wikipedia.org/wiki/E.164>

extend: java.text.Format
<http://java.sun.com/javase/6/docs/api/java/text/Format.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top