我有一个表单,填写的字段是我想要控制的邮件,以便输入地址与标准邮件一致
responsableTechnique.setMail(mailResponsable.getText());
答案 0 :(得分:0)
您可以使用正则表达式,例如:
public static void main(String[]args){
String regex = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)+$";
Pattern.matches(regex,"test@test.fr"); //True
Pattern.matches(regex,"test.test@test.fr"); //True
Pattern.matches(regex,"testtest.fr"); //False
Pattern.matches(regex,"test.test@testfr"); //False
Pattern.matches(regex,"test@test."); //False
Pattern.matches(regex,"test@te@st.fr"); //False
}
所以对你的例子来说:
String regex = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)+$";
String mail = mailResponsable.getText();
if(Pattern.matches(regex,mail)) responsableTechnique.setMail(mail);