JS进行表单验证

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
    function checkDatas(obj){
        if(obj.username.value=='' || obj.password.value==''){
            alert('用户名或密码不可为空!');
            return false;
        }else{
            if(obj.password.value!==obj.rpassword.value){
                alert('请确认两次输入密码是否一致!');
                return false;
            }else{
                return true;
            }
        }
    }
</script>
</head>

<body>
    <form action="./2.php" method="post" onsubmit="return checkDatas(this);">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username" value="" /></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="password" value="" /></td>
            </tr>
            <tr>
                <td>确认密码:</td>
                <td><input type="password" name="rpassword" value="" /></td>
            </tr>
            <tr>
                <td>仅可输入小数点和数字:</td>
                <td><input type="text" name="num01" value="" onkeyup="this.value=this.value.replace(/[^\d\.]/g,'');" /></td>
            </tr>
            <tr>
                <td>仅可输入负号、小数点和数字:</td>
                <td><input type="text" name="num02" value="" onkeyup="this.value=this.value.replace(/[^\d\.\-]/g,'');" /></td>
            </tr>
        </table>

        <input type="submit" value="登录">

    </form>
</body>
</html>

 

posted @ 2018-08-03 17:06  Autumn_n  阅读(149)  评论(0编辑  收藏  举报
TOP