Now validation of Email address and Mobile Number in Java Swing is very easy.
In Swing we take input from user in textfields, and to validate that input such as email and Mobile number
here is the small code:
let say your textfield name for Email and Mobile number field is "txtEmail" and "txtMobile" then
String email = txtEmail.getText();
String mobNumber = txtMobile.getText();
boolean testEmail, testMob;
Pattern patternEmail, patternMob;
Matcher matcherEmail, matcherMob;
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private static final String MOBILE_PATTERN = "\\d{10}";
patternEmail = Pattern.compile(EMAIL_PATTERN);
matcherEmail = patternEmail.matcher(email);
testEmail = return matcherEmail.matches();
patternMob = Pattern.compile(MOBILE_PATTERN);
matcherMob = patternMob.matcher(mobNumber);
testMob = return matcherMob.matches();
now based on the value of testEmail and testMob, you can display an error message to user using
JOptionPane.showMessageDialog(null, "Enter Email in correct format");
Hope this code is useful to you.
Thanks.