Need help with regex please  
Author Message
CodeGrommet





PostPosted: 2008-1-2 22:50:00 Top

java-programmer, Need help with regex please 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
 
-Rick-





PostPosted: 2008-1-3 0:06:00 Top

java-programmer >> Need help with regex please On Jan 2, 4:50 pm, CodeGrommet <email***@***.com> wrote:
> 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;
 
Jeff Higgins





PostPosted: 2008-1-3 0:20:00 Top

java-programmer >> Need help with regex please
CodeGrommet wrote:
> 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>


 
 
Jeff Higgins





PostPosted: 2008-1-3 1:05:00 Top

java-programmer >> Need help with regex please
Jeff Higgins wrote:
>
> 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>
>
>

Or javax.swing.text.MaskFormatter,
depending on your use case.

<http://java.sun.com/javase/6/docs/api/javax/swing/text/MaskFormatter.html>


 
 
Jeff Higgins





PostPosted: 2008-1-3 1:27:00 Top

java-programmer >> Need help with regex please
> Jeff Higgins wrote:
>>
>
> Or javax.swing.text.MaskFormatter,
> depending on your use case.
>
> <http://java.sun.com/javase/6/docs/api/javax/swing/text/MaskFormatter.html>

Formatting Basics with JFormattedTextField by John Zukowski:
<http://java.sun.com/developer/onlineTraining/new2java/supplements/2005/May05.html#1>