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- - 02 Jan 2008 16:06 GMT
> 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
[quoted text clipped - 19 lines]
> 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 - 02 Jan 2008 16:20 GMT
> 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
[quoted text clipped - 16 lines]
> 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 - 02 Jan 2008 17:04 GMT
> 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 - 02 Jan 2008 17:27 GMT
> 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>