我不知道我的代码有什么问题,我只想接受这种格式的手机号码:09xxxxxxxxx(始终以“09”开头,总共 11 位数字)。所有努力将不胜感激。提前致谢。
Here is the picture of the problem
代码如下:
String a2= jTextField6.getText();
String a3 = jTextField7.getText();
Pattern p = Pattern.compile("^(09) \\d {9}$");
Matcher m = p.matcher(jTextField5.getText());
if (!m.matches()){
int b = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(this, "Invalid Mobile Number", "Error", b);
return;
}
if (null==a2||a2.trim().isEmpty()){
int b = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(this, "Fields should not left blank", "Error", b);
return;
}
if(a3==null||a3.trim().isEmpty()){
int b = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(this, "Fields should not left blank", "Error", b);
}
else {
int c = JOptionPane.YES_NO_OPTION;
int d = JOptionPane.showConfirmDialog(this, "Confirm Purchase?","Costume 1", c);
if (d==0){
JOptionPane.showMessageDialog( null,"Your costume will be delivered 3-5 working days." +"\n"+"\n"+" Thank You!");
}
最佳答案
您必须删除正则表达式中的空格:
Pattern p = Pattern.compile("^(09)\\d{9}$");
否则它们将被视为必须存在的角色。
关于java - 手机号码验证问题的正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52345158/