js正则表达式 校验邮箱,非法字符限制输入
Email地址的格式验证规则
JS代码格式:
var pattern = /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
var str = "xindd123456@163.com";
console.log(pattern.test(str));
Java代码格式:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches {
public static void main(String args[]) {
String str = "xindd123456@163.com";
String pattern = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
System.out.println(m.matches());
}
}
不能输入!@#¥%……&* 这种字符的正则表达式书写:
//邮箱格式验证
function checkEmail(email){
var emailReg = /[@#\$%\^&\*]+/g;
if (email!=null && email!='' && emailReg.test(email) == true) {
alert("邮箱格式不正确!");
return false;
} else {
return true;
}
}