电话号码应包含10位数字。如果超过10位数,则应显示错误消息
String user = txtName.getText();
if(!user.matches("[a-zA-Z\\s]*")) {
JOptionPane.showMessageDialog(null, "Invalid Name format !! \nPlease enter string only !!!");
}
答案 0 :(得分:0)
private static boolean validatePhoneNumber(String phoneNo) {
//validate phone numbers of format "1234567890"
if (phoneNo.matches("\\d{10}")) return true;
//validating phone number with -, . or spaces
else if(phoneNo.matches("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}")) return true;
//validating phone number with extension length from 3 to 5
else if(phoneNo.matches("\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}")) return true;
//validating phone number where area code is in braces ()
else if(phoneNo.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) return true;
//return false if nothing matches the input
else return false;
}
来源信用:http://www.journaldev.com/641/regular-expression-phone-number-validation-in-java
答案 1 :(得分:-1)
你好看Validate International Phone Numbers
这是如何使用正则表达式验证国际电话号码。