JS项目(设计一个包含用户名、密码、重复密码、年龄等栏目的表单,并对输入的密码和重复密码进行一致性验证和保证输入的年龄是数字进行验证)

JS小项目

  • 设计一个包含用户名、密码、重复密码、年龄等栏目的表单,并对输入的密码和重复密码进行一致性验证和保证输入的年龄是数字进行验证。

代码、效果图附下:(直接复制就能用)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单验证</title>

</head>
<body>
    <form name="formWork">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" placeholder="用户名长度为6-14字符" id="username" name="username" onblur="checkName(this.value)" onfocus="clearNameError()" /></td>
				<td><span id="nameError"></span></td>
            </tr>
			<tr>
			    <td>年龄:</td>
			    <td><input type="text" placeholder="年龄必须为数字" id="age" name="age" onblur="checkAge(this.value)" onfocus="clearAgeError()" /></td>
			    <td><span id="AgeError"></span></td>
			</tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" placeholder="密码不能少于6个字符" id="userpswd" name="userpswd" onblur="checkUserpswd(this.value)" onfocus="clearPswdError()" /></td>
                <td><span id="pswdError"></span></td>
            </tr>
            <tr>
                <td>确认密码:</td>
                <td><input type="password" placeholder="请再输入一次" id="confirmpswd" name="confirmpswd" onblur="isSame(this.value);" onfocus="clearSameError()" /></td>
                <td><span id="confirmpswdError"></span></td>
            </tr>
            <tr>
                <td><input type="button" value="注册" onclick="checkAll();"/></td>
            </tr>
        </table>
    </form>
	
	
    <script type="text/javascript">
        String.prototype.trim = function() {
            return this.replace(/CDSN:axunla(^\s*)|(\s*$)/g, "");
        }

        function checkName(username) {
            username = username.trim();
            var ok = false;
            var nameError = document.getElementById("nameError");
            if (username == "") {
                nameError.innerHTML = "<font color='red'>用户名不能为空 </font>";
                ok = false;
            } else if (username.length < 6 || username.length > 14) {
                nameError.innerHTML = "<font color='red'>用户名长度6-14 </font>";
                ok = false;
            } else {
                nameError.innerHTML = " ";
                ok = true;
            }
            return ok;
        }

        function clearNameError() {
            var nameError = document.getElementById("nameError");
            nameError.innerHTML = "";
        }
		function checkAge(age){
			age = age.trim();
			var ok1 = false;
			var AgeError = document.getElementById("AgeError");
			if (isNaN(age)) {
			    AgeError.innerHTML = "<font color='red'>你输入的年龄不是数字,请重新输入</font>";
			    ok1 = false;
            } else if(age == ""){
				AgeError.innerHTML = "<font color='red'>年龄不能为空 </font>";
				ok1 = false;
			} else {
			    AgeError.innerHTML = " ";
			    ok1 = true;
			}
			return ok1;
		}
		function clearAgeError(){
			var AgeError = document.getElementById("AgeError");
			AgeError.innerHTML="";
		}

        function checkUserpswd(userpswd) {
            var pswdError = document.getElementById("pswdError");
            var ok = false;
            if (userpswd == "") {
                pswdError.innerHTML = "<font color='red'>密码不能为空 </font>";
                ok = false;

            } else if (userpswd.length < 6) {
                pswdError.innerHTML = "<font color='red'>密码不能少于6位 </font>";
                ok = false;

            } else {
                pswdError.innerHTML = "";
                ok = true;

            }
            return ok;
        }

        function clearPswdError() {
            var pswdError = document.getElementById("pswdError");
            pswdError.innerHTML = "";
        }

        function isSame() {
            var userpswd = document.getElementById("userpswd").value;
            var confirmpswd = document.getElementById("confirmpswd").value;
            var confirmpswdError = document.getElementById("confirmpswdError");
            var ok = false;

            if (userpswd != confirmpswd) {
                confirmpswdError.innerHTML = "<font color='red'>密码不一致</font>";
                ok = false;

            } else {
                confirmpswdError.innerHTML = '';
                ok = true;

            }
            return ok;
        }

        function clearSameError() {
            var confirmpswdError = document.getElementById("confirmpswdError");
            confirmpswdError.innerHTML = '';
        }

        function checkAll() {
            var username = document.getElementById("username").value;
			var age = document.getElementById("age").value;
            var userpswd = document.getElementById("userpswd").value;
            if (checkName(username) && checkUserpswd(userpswd) && checkAge(age) && isSame()) {
                document.formWork.submit();
				alert("注册成功");
            }
        }
    </script>
</body>
</html>


效果图如下:

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿勋啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值