使用JQUERY实现邮箱验证

来源:互联网 发布:炉石传说代练金币淘宝 编辑:程序博客网 时间:2024/05/16 00:51
<head>
    <title></title>
    <style type="text/css">
    .txtbackcolor{background-color:#eee;}
    </style>
    <script src="jquery-1.9.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var state = false;
            $("#txtEmail").focus(function () {
                if (state == false) {
                    $(this).val('');
                }
            })
            $("#txtEmail").blur(function () {
                if ($(this).val() == '') {
                    $("#spinfo").text("邮箱不能为空");
                    $(this).focus();
                }
                else {
                    if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($(this).val()) == false) {
                        $("#spinfo").text("邮箱格式不正确,请重新填写");
                        $(this).focus();
                    }
                    else {
                        $("#spinfo").text('');
                        $("#spinfo").append("<img src=images/onSuccess.gif />");
                        state=true;
                    }
                }
            })
        })
    </script>
</head>
<body>

 邮箱<input id="txtEmail" type="text" value="输入邮箱" class="txtbackcolor" /><span id="spinfo"></span><br /></body>



//jquery验证邮箱 
    function checkSubmitEmail(){ 
       if($("#email").val()==""){ 
    $("#confirmMsg").html("<font color='red'>邮箱地址不能为空!</font>"); 
    $("#email").focus(); 
    return false; 
   } 
   if(!$("#email").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)){ 
    $("#confirmMsg").html("<font color='red'>邮箱格式不正确!请重新输入!</font>"); 
    $("#email").focus(); 
    return false; 
   } 
   return true; 
    } 
    
    //jquery验证手机号码 
    function checkSubmitMobil(){ 
       if($("#mobil").val()==""){ 
    $("#moileMsg").html("<font color='red'>手机号码不能为空!</font>"); 
    $("#mobil").focus(); 
    return false; 
   } 

   if(!$("#mobil").val().match(/^1[3|4|5|8][0-9]\d{4,8}$/)){ 
    $("#moileMsg").html("<font color='red'>手机号码格式不正确!请重新输入!</font>"); 
    $("#mobil").focus(); 
    return false; 
   } 
   return true; 
    }

0 0
原创粉丝点击