日期:2014-05-16  浏览次数:20343 次

js验证
一般情况下,在验证前都要先去除空格、回车换行
js去除空格、回车换行代码
var emails = $("#emails").val();
emails = emails.replace(/\ +/g,"");
emails = emails.replace(/[ ]/g,"");
emails = emails.replace(/<\/?.+?>/g,"");
emails = emails.replace(/[\r\n]/g, "");
alert(emails);


1,验证邮箱
var email = $("#email").val();
var normalMail = new RegExp(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/g);
if(!normalMail.test(email)){
    alert("邮箱地址格式不正确");
    return false;
}


注意如果多个邮箱验证,
var emails = $("#emails").val();
var emailsArr = emails.split(",");
for(var i = 0; i < emailsArr.length; i++){
    var normalMail = new RegExp(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/g);
    if(!normalMail.test(emailsArr[i])){
        $(".errorSummary").text(cur_mail+"邮箱地址格式不正确!").show();
        return false;
     }
}



2,验证时间格式
var _reTimeReg = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;
if(!_reTimeReg.test(visit_time)){
    alert("请使用日历控件,输入格式正确的日期时间!");
    return false;
}

参考地址:http://blog.csdn.net/flm_0722/article/details/6620075

验证日期格式