如何验证电子邮件字段输入
我已经将电子邮件字段编码为超过 14 个字符,以“.com”结尾,并在“.”旁边加上“@”。
String regemail = emailfield.getText();
if ( !regemail.contains("@") ) {
JOptionPane.showMessageDialog(null, "Email must contain '@' once");
emailfield.requestFocus();
return false;
} else
if ( !regemail.contains(".com") ) {
JOptionPane.showMessageDialog(null, "Email must contain '.com'
once");
emailfield.requestFocus();
return false;
} else
if ( regemail.indexOf("@")
> regemail.lastIndexOf(".com") ) {
JOptionPane.showMessageDialog(null, "Wrong Email Format");
emailfield.requestFocus();
return false;
} else
if(regemail.length() < 13) {
JOptionPane.showMessageDialog(null, "Email length must more
than 14 characters");
emailfield.requestFocus();
return false;
}
请使用正则表达式:
private final Pattern EMAIL_ADDRESSS_REGEX_PATTERN =
Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
private boolean isValidEmail(String email)
{
return EMAIL_ADDRESSS_REGEX_PATTERN.matcher(email).find();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句