jsp中email格式限制问题!sun给的例子便宜不通!!!
大家好!下面是老师给的sun的例子,我没改过! 但是为什么编译不同呢?谢谢大家帮忙看以下!
/*
* Checks for invalid characters
* in email addresses
*/
public class EmailValidation {
public static void main(String[] args)
throws Exception {
try{
String input = "123@sun.com ";
//Checks for email addresses starting with
//inappropriate symbols like dots or @ signs.
Pattern p = Pattern.compile( "^\\.|^\\@ ");
Matcher m = p.matcher(input);
if (m.find())
System.err.println( "Email addresses don 't start " +
" with dots or @ signs. ");
//Checks for email addresses that start with
//www. and prints a message if it does.
p = Pattern.compile( "^www\\. ");
m = p.matcher(input);
if (m.find()) {
System.out.println( "Email addresses don 't start " +
" with \ "www.\ ", only web pages do. ");
}
p = Pattern.compile( "[^A-Za-z0-9\\.\\@_\\-~#]+ ");
m = p.matcher(input);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result) {
deletedIllegalChars = true;
m.appendReplacement(sb, " ");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
input = sb.toString();
if (deletedIllegalChars) {
System.out.println( "It contained incorrect characters " +
" , such as spaces or commas. ");
}
}catch(Exception e){System.out.print(e.toString());}
}
}
[解决办法]
这种验证别进到后台在验证,在页面就先过滤掉,这里是个比较简单的JS验证
function checkEmail(){
var email=t_hotel_hnmc.f_SAFETY_EMAIL_HOTE.value;
var filter=/^[A-Za-z0-9][A-Za-z0-9_-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
if (email.length> 0)
{
if (filter.test(email))
return true;
else
alert( "请输入一个合法的电子信箱地址! " );
}
return false;
}