0

Having trouble using a regex to validate a phone number. I want to allow only numbers and hyphens, but so far I've been trying to just get numbers working. Unfortunately despite different changes after looking around Google and Stack Overflow, the regex continues to evaluate as false. Ideally, strings such as 8889990000 or 888-999-3333 should both evaluate to true. Any help is appreciated!

Main class code for regex:

boolean correct = PhoneFilter.filterPhone(phone_num);

if (correct == true) {
    //create an intent to carry data from main activity to OrderConfirmationActivity
    Intent intent = new Intent(MainActivity.this, OrderConfirmationActivity.class);

    //pack pizza order data into intent
    intent.putExtra("nameSelected", nameEditText.getText().toString());
    intent.putExtra("aVariable", type);
    intent.putExtra("mtSelected", mt);
    intent.putExtra("otSelected", ot);
    intent.putExtra("ptSelected", pt);
    intent.putExtra("dateSelected", Date);

    //start the OrderConfirmationActivity
    startActivity(intent);


}
        //alert user if phone number entered incorrectly
else if (correct == false) {
    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
    alertDialog.setTitle("Alert");
    alertDialog.setMessage("Please enter a phone number in either 000-0000-000 format or 0000000000 format");
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    alertDialog.show();
}

The code for filterPhone is:

public class PhoneFilter {

    public static boolean filterPhone(String phone_text) {
        boolean correct;

        if ((phone_text.length() <= 12) && (phone_text.matches("[0-9]+")))
            correct = true;
        else
            correct = false;

        System.out.println("correct =" + correct);
        return correct;

        //InputFilter lengthFilter = new InputFilter.LengthFilter(12);
    }
}
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Matt
  • 81
  • 2
  • 11
  • This may be worth reading: http://howtodoinjava.com/2014/11/12/java-regex-validate-international-phone-numbers/ – Paulo Avelar Nov 03 '15 at 03:19
  • Turns out that the regex was not the problem, or at least only part of the problem, apparently I had forgotten to add `.getText()` when assigning the editText contents to a string variable. – Matt Nov 03 '15 at 04:05

1 Answers1

1

Try switching

phone_text.matches("[0-9]+")

to

phone_text.matches("^[0-9-]+$")

I put together a quick test to try it out:

public static void main(String[] args) {
    //Sysout for example only
    System.out.println(filterPhone("8889990000"));
    System.out.println(filterPhone("888-999-3333"));
    System.out.println(filterPhone("888B999A3333"));
    System.out.println(filterPhone(""));
}

public static boolean filterPhone(String phone_text) {
    boolean correct;

    if ((phone_text.length() <= 12) && (phone_text.matches("^[0-9-]+$")))
        correct = true;
    else
        correct = false;

    System.out.println("correct =" + correct);
    return correct;

    // InputFilter lengthFilter = new InputFilter.LengthFilter(12);
}

Produces:

correct =true
true
correct =true
true
correct =false
false
correct =false
Nick DeFazio
  • 2,412
  • 27
  • 31
  • I'm getting the same output you have, but when I try to run it using the string from my edittext it still evaluates as false. I'll continue to look through it, maybe I have a syntax error somewhere... – Matt Nov 03 '15 at 03:52
  • Ah, fixed the problem, needed `.getText()` when assigning the contents of editText to a string. Thanks for helping me figure out how correctly use the regex though! – Matt Nov 03 '15 at 04:06
  • No problem! Happy to help. – Nick DeFazio Nov 03 '15 at 04:52