JS開發驗證表單教程之驗證表單(四)

上一節我們講到了控制用戶名的長度

下面我們來看一下透過js的正規表示式來控制密碼的格式與郵箱

即使你不會寫正則表達式也沒關係,現在在網路上能找到很多現成寫好的郵箱的正規表示式

我們來看下密碼,例如我只要個數字和字母組合的密碼控制在6到10位

var rel = /^[a-zA-Z0-9]{6,10}$/ ;

我們來看下信箱的正規表示式

var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+) *\.[A-Za-z0-9]+$/;

#正規我們就已經完成了,那麼如何驗證我們的表單?

我們要取得文字方塊的值,然後跟我們的正規表示式進行匹配,如果匹配,則是符合規範的,不匹配,給出提示訊息

這裡我們將要使用test() 方法

test() 方法用來檢索字串是否符合某個模式

我們來驗證密碼,是數字與字母的組合

var rel = /^[A-Za-z0-9]{6,10}$/;
            var val1 = document.getElementById('pwd       ;# #                document.getElementById("sp1").innerHTML = "請輸入密碼!";
            document.getElementById("sp1").innerHTML = "密碼不符合規格!
#var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za- z0-9]+)*\.[A-Za-z0-9]+$/;
            var val2 = document.getElementById('e    {
                document.getElementById("sp2").innerHTML = "請輸入信箱!";
           document.getElementById("sp2"). innerHTML = "郵箱格式不正確!";

            }

如上程式碼,我們就完成了驗證郵箱的功能

完整程式碼如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
        *{margin:0;padding:0;}
        #div{width:410px;height:400px;background:#46a3ff;padding-left:16px;
            padding-top:20px;}
        input{
            outline:none;
            box-sizing:border-box;padding-left:15px;}
        textarea{
            outline:none;resize : none;
            box-sizing:border-box;padding-left:15px;padding-top:5px;}
        .name{width:200px;height:30px;margin-left:8px;border:1px solid #ffc1e0;border-radius:8px;}
        .pwd{width:200px;height:30px;
            margin-left:8px;border:1px solid #ffc1e0;border-radius:8px;}
        .email{width:200px;height:30px;margin-left:8px;border:1px solid #ffc1e0;border-radius:8px;}
        .txt{
            width:280px;height:70px;margin-left:8px;border:1px solid #ffc1e0;border-radius:8px;}
        .sub{width:100px;height:30px;padding-left:0px;
            border:none;
            border-radius:5px;background:#ffd0ff;}
        .sub:hover{background:#ffaad5;}
        .div{
            width:200px;height:30px;margin:0 auto;box-sizing:border-box;padding-left:45px;padding-top:5px;color:#8600ff;font-weight:bold;}

    </style>
</head>
<body>
    <div id="div">
        <form>
            <label>用户名:</label>
            <input type="text" class="name" id="name">
            <div id="sp" class="div"></div>                
            <label>密&nbsp;码:</label>
            <input type="password" class="pwd" id="pwd">
            <div id="sp1" class="div"></div>
            <label>邮&nbsp;箱:</label>
            <input type="text" class="email" id="email">
            <div id="sp2" class="div"></div>
            <label>爱&nbsp;好:</label>
            <textarea rows="5" cols="40" class="txt" id="txt"></textarea>
            <div id="sp3" class="div"></div>
            <input type="button" class="sub" value="注册" id="sub">
        </form>
    </div>

    <script type="text/javascript">
        var sub = document.getElementById('sub');
        sub.onclick=function(){
            //验证用户名是否为空,如果为空,给出提示信息
            var val = document.getElementById('name').value;
            var rel = document.getElementById('name').value.length;
            //console.log(val.length);
            if(val == ""){
                document.getElementById("sp").innerHTML = "用户名不能为空!";
            }else if( rel <= 5 || rel > 10){
                document.getElementById("sp").innerHTML = "格式不正确!";
            }
            var rel = /^[A-Za-z0-9]{6,10}$/;
            var val1 = document.getElementById('pwd').value;
            if(val1 == ""){
                document.getElementById("sp1").innerHTML = "请输入密码!";
            }else if(!rel.test(val1)){
                document.getElementById("sp1").innerHTML = "密码不符合规范!";
            }
            //验证邮箱是否为空和邮箱的格式是否正确
            var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
            var val2 = document.getElementById('email').value;
            if(val2 == ""){
                document.getElementById("sp2").innerHTML = "请输入邮箱!";
            }else if(!reg.test(val2)){
                document.getElementById("sp2").innerHTML = "邮箱格式不正确!";
            }
            //验证内容是否为空
            var val3 = document.getElementById('txt').value;
            if(val3 == ""){
                document.getElementById("sp3").innerHTML = "请输入内容!";
            }
        }

            //当用户名的文本框触发键盘事件,提示信息去除
            document.getElementById('name').onkeyup=function(){
                document.getElementById('sp').innerHTML = " ";
            }
            // //当密码框触发键盘事件,提示信息去除
            document.getElementById('pwd').onkeyup=function(){
                document.getElementById('sp1').innerHTML = " ";
            }
            // //当邮箱文本框触发键盘事件,提示信息去除
            document.getElementById('email').onkeyup=function(){
                document.getElementById('sp2').innerHTML = " ";
            }
            // //当文本域触发键盘事件,提示信息去除
            document.getElementById('txt').onkeyup=function(){
                document.getElementById('sp3').innerHTML = " ";
            }            

    </script>
</body>
</html>

#rrreee
#rrreee
#
繼續學習
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{margin:0;padding:0;} #div{width:410px;height:400px;background:#46a3ff;padding-left:16px; padding-top:20px;} input{ outline:none; box-sizing:border-box;padding-left:15px;} textarea{ outline:none;resize : none; box-sizing:border-box;padding-left:15px;padding-top:5px;} .name{width:200px;height:30px;margin-left:8px;border:1px solid #ffc1e0;border-radius:8px;} .pwd{width:200px;height:30px; margin-left:8px;border:1px solid #ffc1e0;border-radius:8px;} .email{width:200px;height:30px;margin-left:8px;border:1px solid #ffc1e0;border-radius:8px;} .txt{ width:280px;height:70px;margin-left:8px;border:1px solid #ffc1e0;border-radius:8px;} .sub{width:100px;height:30px;padding-left:0px; border:none; border-radius:5px;background:#ffd0ff;} .sub:hover{background:#ffaad5;} .div{ width:200px;height:30px;margin:0 auto;box-sizing:border-box;padding-left:45px;padding-top:5px;color:#8600ff;font-weight:bold;} </style> </head> <body> <div id="div"> <form> <label>用户名:</label> <input type="text" class="name" id="name"> <div id="sp" class="div"></div> <label>密 码:</label> <input type="password" class="pwd" id="pwd"> <div id="sp1" class="div"></div> <label>邮 箱:</label> <input type="text" class="email" id="email"> <div id="sp2" class="div"></div> <label>爱 好:</label> <textarea rows="5" cols="40" class="txt" id="txt"></textarea> <div id="sp3" class="div"></div> <input type="button" class="sub" value="注册" id="sub"> </form> </div> <script type="text/javascript"> var sub = document.getElementById('sub'); sub.onclick=function(){ //验证用户名是否为空,如果为空,给出提示信息 var val = document.getElementById('name').value; var rel = document.getElementById('name').value.length; //console.log(val.length); if(val == ""){ document.getElementById("sp").innerHTML = "用户名不能为空!"; }else if( rel <= 5 || rel > 10){ document.getElementById("sp").innerHTML = "格式不正确!"; } var rel = /^[A-Za-z0-9]{6,10}$/; var val1 = document.getElementById('pwd').value; if(val1 == ""){ document.getElementById("sp1").innerHTML = "请输入密码!"; }else if(!rel.test(val1)){ document.getElementById("sp1").innerHTML = "密码不符合规范!"; } //验证邮箱是否为空和邮箱的格式是否正确 var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; var val2 = document.getElementById('email').value; if(val2 == ""){ document.getElementById("sp2").innerHTML = "请输入邮箱!"; }else if(!reg.test(val2)){ document.getElementById("sp2").innerHTML = "邮箱格式不正确!"; } //验证内容是否为空 var val3 = document.getElementById('txt').value; if(val3 == ""){ document.getElementById("sp3").innerHTML = "请输入内容!"; } } //当用户名的文本框触发键盘事件,提示信息去除 document.getElementById('name').onkeyup=function(){ document.getElementById('sp').innerHTML = " "; } // //当密码框触发键盘事件,提示信息去除 document.getElementById('pwd').onkeyup=function(){ document.getElementById('sp1').innerHTML = " "; } // //当邮箱文本框触发键盘事件,提示信息去除 document.getElementById('email').onkeyup=function(){ document.getElementById('sp2').innerHTML = " "; } // //当文本域触发键盘事件,提示信息去除 document.getElementById('txt').onkeyup=function(){ document.getElementById('sp3').innerHTML = " "; } </script> </body> </html>