regex - How to validate phone number in java using regular expression -


this question has answer here:

phone number should contain 10 digit. if exceeds more 10 digit error message should displayed

string user = txtname.gettext(); if(!user.matches("[a-za-z\\s]*")) {     joptionpane.showmessagedialog(null, "invalid name format !! \nplease enter string !!!"); } 

private static boolean validatephonenumber(string phoneno) {         //validate phone numbers of format "1234567890"         if (phoneno.matches("\\d{10}")) return true;         //validating phone number -, . or spaces         else if(phoneno.matches("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}")) return true;         //validating phone number extension length 3 5         else if(phoneno.matches("\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}")) return true;         //validating phone number area code in braces ()         else if(phoneno.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) return true;         //return false if nothing matches input         else return false;      } 

source credit: http://www.journaldev.com/641/regular-expression-phone-number-validation-in-java


Comments