Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

Need help with regex please

Thread view: 
CodeGrommet - 02 Jan 2008 14:50 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
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>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.