user3537993
user3537993

Reputation: 29

java.util.regex Validating Phone numbers

I am trying to validate a phone number input from a user(in my project). This is the method I created trying to validate it and also give them 3 tries to enter a valid format. However what ever I cannot get it to return the prompt that tells them to re enter the number it just runs it true or false. Where am I going wrong.

public static boolean getPhoneInput(String phone) {
int count =0;
String input,
inputString = phone;          
input = JOptionPane.showInputDialog( phone );
String pattern="(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}";               
Pattern p = Pattern.compile(pattern);       
Matcher m = p.matcher(inputString);  
     while ((phone != null && input.length() == 0) && (count <2)){
         input = JOptionPane.showInputDialog("No input was detected \n" + phone);
         count++;}

     if (count==2){
            JOptionPane.showMessageDialog(null, " Sorry due to errors your order cannot be processed.  GOODBYE.");
            System.exit(0);      {           

      }
    }
    return false;} 

I tried to use an example I found and modify it for my program. I am unsure how to get the Boolean type to return a message if they leave the field blank or in an invalid format.

I was able to get everything working like a charm within my code using the getPhoneInput() method, however is there a way to return something other than a true or false with a Boolean. I understand that is what it was built to do, but once I have the correct format i want the number written to a txt file. as it works now instead of logging a number with the users name it just tells me that their phone number matched the required format or that it did not with a true or false.

Upvotes: 0

Views: 532

Answers (1)

Azar
Azar

Reputation: 1106

I've not thoroughly tested this, but it should work for you:

public static boolean getPhoneInput(String displayText)
{
    int count = 0;
    String input;
    input = JOptionPane.showInputDialog(displayText);
    String pattern = "\\d-(\\d{3})-(\\d{3})-(\\d{4})";
    Pattern p = Pattern.compile(pattern);
    while ((displayText != null && input.length() == 0))
    {
        input = JOptionPane.showInputDialog("No input was detected \n" + displayText);
        count++;
        if (count == 2)
        {
            JOptionPane.showMessageDialog(null, " Sorry due to errors your order cannot be processed.  GOODBYE.");
            System.exit(0);
        }
    }
    count = 0;
    while (count <= 3)
    {
        count++;
        Matcher m = p.matcher(input);
        if (m.matches())
            return true;
        else
            input = JOptionPane.showInputDialog("Input in wrong format. \n" + displayText);
    }
    return false;
}

Changes Made:

  1. Moved if statement to inside the while loop, changed while condition, as if statement will already cause the loop to exit if count is 2.
  2. Changed your regex (moved dashes outside of parens, got rid of ?)
  3. Added an actual check to see if the string matched the pattern.
  4. Moved Matcher m = p.matcher(input); to end of method, otherwise an initial empty input will always return false.
  5. inputString was superfluous.
  6. You had an extra set of braces following the if that served no discernible purpose.
  7. Added extra validation step.

I'd also like to point out that you should probably not use this regex for any real world applications, as it only validates one, very specific, form of phone number.

Upvotes: 1

Related Questions