解惑(2)javascript 判断用户输入文本框是否为空

举个之前写的例子

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>注册</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="css/basic.css"/>
    <link href="css/login.css" rel="stylesheet" type="text/css">
</head>

<body>
        <div class="mr-tabs" id="doc-my-tabs">
                <ul class="mr-tabs-nav mr-nav mr-nav-tabs mr-nav-justify">
                    <li class="mr-active"><a href="">注册</a></li>
                </ul>

                <div class="mr-tabs-bd">
                    <div class="mr-tab-panel mr-active">
                    	
                        <form method="post">
                            <div class="user-email">
                                <label for="email"><i class="mr-icon-envelope-o"></i></label>
                                <input type="email" name="" id="email" placeholder="请输入邮箱账号">
                            </div>
                            <div class="user-pass">
                                <label for="password"><i class="mr-icon-lock"></i></label>
                                <input type="password" name="" id="password" placeholder="设置密码">
                            </div>
                            <div class="user-pass">
                                <label for="passwordRepeat"><i class="mr-icon-lock"></i></label>
                                <input type="password" name="" id="passwordRepeat" placeholder="确认密码">
                            </div>
                            <div class="user-pass">
                                <label for="passwordRepeat"><i class="mr-icon-mobile"></i><span style="color:red;margin-left:5px">*</span></label>
                                <input type="text" name="" id="tel" placeholder="请输入手机号">
                            </div>
                        </form>

                        <div class="login-links">
                            <label for="reader-me">
                                <input id="reader-me" type="checkbox"> 点击表示您同意商城《服务协议》
                            </label>
                            <a href="login.html" class="mr-fr">登录</a>
                        </div>
                        
                        <div class="mr-cf">
                            <input type="submit" name="" onclick="mr_verify()" value="注册" class="mr-btn mr-btn-primary mr-btn-sm mr-fl">
                        </div>
             </div>
           </div>
        </div>
</body>
<script>
    function mr_verify(){

        //获取表单对象
        var email=document.getElementById("email");
        var password=document.getElementById("password");
        var passwordRepeat=document.getElementById("passwordRepeat");
        var tel=document.getElementById("tel");

        //验证项目是否为空
        if(email.value==='' || email.value===null){
            alert("邮箱不能为空!");
            return;
        }
        if(password.value==='' || password.value===null){
            alert("密码不能为空!");
            return;
        }
        if(passwordRepeat.value==='' || passwordRepeat.value===null){
            alert("确认密码不能为空!");
            return;
        }
        if(tel.value==='' || tel.value===null){
            alert("手机号码不能为空!");
            return;
        }

        if(password.value!==passwordRepeat.value ){
            alert("密码设置前后不一致!");
            return;
        }
        
        //验证邮件格式
        apos = email.value.indexOf("@")
        dotpos = email.value.lastIndexOf(".")
        if (apos < 1 || dotpos - apos < 2) {
            alert("邮箱格式错误!");
        }
        else {
            alert("邮箱格式正确!");
        }
        //验证手机号格式
        if(isNaN(tel.value)){
            alert("手机号请输入数字!");
            return;
        }
        if(tel.value.length!==11){
            alert("手机号是11个数字!");
            return;
        }
        alert('注册成功!');
    }
    
</script>
</html>

 

猜你喜欢

转载自blog.csdn.net/Smtime826/article/details/84583523