注册验证(jquery)

表单

<style type="text/css">
		span{
			color: red;
		}
</style>
<form action="http://localhost:8080">
			<table>
				<tr>
					<td>用户名称:</td>
					<td>
						<input type="text" id="name" placeholder="请输入用户名" />
					</td>
					<td>
						<span id="namespan"></span>
					</td>
				</tr>
				<tr>
					<td>用户密码:</td>
					<td>
						<input type="password" id="password" placeholder="请输入用户密码" />
					</td>
					<td>
						<span id="passwordspan"></span>
					</td>
				</tr>
				<tr>
					<td>确认密码:</td>
					<td>
						<input type="password" id="repassword" placeholder="请输入确认密码" />
					</td>
					<td>
						<span id="repasswordspan"></span>
					</td>
				</tr>
				<tr>
					<td>用户邮箱:</td>
					<td>
						<input type="text" id="email" placeholder="请输入用户邮箱" />
					</td>
					<td>
						<span id="emailspan"></span>
					</td>
				</tr>
				<tr>
					<td>用户地址:</td>
					<td>
						<input type="text" id="adress" placeholder="请输入用户地址" />
					</td>
					<td>
						<span id="adressspan"></span>
					</td>
				</tr>
			</table>
			<div>
				<button type="submit" id="sub" >注册</button>
			</div>
		</form>

效果图
在这里插入图片描述

点击验证

用户名验证
<script type="text/javascript" src="./jquery/jquery-1.7.2.js"></script>
	<script type="text/javascript">
		$(function () {
			$("#sub").click(function () {
				//验证用户名
				var nameText=$("#name").val();
				//正则
				patt=/^\w{5,12}$/;
				if (!patt.test(nameText)){
					$("#namespan").text("用户名格式错误!");
					return false;
				}
				//正确要把红字去掉
				$("#namespan").text("");
			});
		});
	</script>

效果图(通过点击后判断)
在这里插入图片描述

用户密码验证
	//验证用户密码
	var passwordText=$("#password").val();
	patt=/^\w{5,12}$/;
	if (!patt.test(passwordText)){
		$("#passwordspan").text("用户密码格式错误!");
		return false;
	}
	//正确要把红字去掉
	$("#passwordspan").text("");
确认密码验证
	//验证确认密码
	var passwordText=$("#password").val();
	var repasswordText=$("#repassword").val();
	if (passwordText!=repasswordText){
		$("#repasswordspan").text("确认密码和用户密码不一致!");
		return false;
	}
	//正确要把红字去掉
	$("#repasswordspan").text("");
用户邮件验证
	//验证邮箱
	var emailText=$("#email").val();
	patt=/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
	if (!patt.test(emailText)){
		$("#emailspan").text("用户邮箱格式错误!");
		return false;
	}
	//正确要把红字去掉
	$("#emailspan").text("");
整体代码
<style type="text/css">
		span{
			color: red;
		}
	</style>
	<script type="text/javascript" src="./jquery/jquery-1.7.2.js"></script>
	<script type="text/javascript">
		$(function () {
			$("#sub").click(function () {
				//验证用户名
				var nameText=$("#name").val();
				patt=/^\w{5,12}$/;
				if (!patt.test(nameText)){
					$("#namespan").text("用户名格式错误!");
					return false;
				}
				//正确要把红字去掉
				$("#namespan").text("");

				//验证用户密码
				var passwordText=$("#password").val();
				patt=/^\w{5,12}$/;
				if (!patt.test(passwordText)){
					$("#passwordspan").text("用户密码格式错误!");
					return false;
				}
				//正确要把红字去掉
				$("#passwordspan").text("");

				//验证确认密码
				var passwordText=$("#password").val();
				var repasswordText=$("#repassword").val();
				if (passwordText!=repasswordText){
					$("#repasswordspan").text("确认密码和用户密码不一致!");
					return false;
				}
				//正确要把红字去掉
				$("#repasswordspan").text("");

				//验证邮箱
				var emailText=$("#email").val();
				patt=/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
				if (!patt.test(emailText)){
					$("#emailspan").text("用户邮箱格式错误!");
					return false;
				}
				//正确要把红字去掉
				$("#emailspan").text("");

			});
		});
	</script>

	<div>
		<form action="http://localhost:8080">
			<table>
				<tr>
					<td>用户名称:</td>
					<td>
						<input type="text" id="name" placeholder="请输入用户名" />
					</td>
					<td>
						<span id="namespan"></span>
					</td>
				</tr>
				<tr>
					<td>用户密码:</td>
					<td>
						<input type="password" id="password" placeholder="请输入用户密码" />
					</td>
					<td>
						<span id="passwordspan"></span>
					</td>
				</tr>
				<tr>
					<td>确认密码:</td>
					<td>
						<input type="password" id="repassword" placeholder="请输入确认密码" />
					</td>
					<td>
						<span id="repasswordspan"></span>
					</td>
				</tr>
				<tr>
					<td>用户邮箱:</td>
					<td>
						<input type="text" id="email" placeholder="请输入用户邮箱" />
					</td>
					<td>
						<span id="emailspan"></span>
					</td>
				</tr>
				<tr>
					<td>用户地址:</td>
					<td>
						<input type="text" id="adress" placeholder="请输入用户地址" />
					</td>
					<td>
						<span id="adressspan"></span>
					</td>
				</tr>
			</table>
			<div>
				<button type="submit" id="sub" >注册</button>
			</div>
		</form>
	</div>

自动验证

用户名验证
//只需要修改第一排
//$("#sub").click(function () {
$("#name").blur(function () {
				//验证用户名
				var nameText=$("#name").val();
				patt=/^\w{5,12}$/;
				if (!patt.test(nameText)){
					$("#namespan").text("用户名格式错误!");
					return false;
				}
				//正确要把红字去掉
				$("#namespan").text("");

整理:
尚硅谷

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用jQuery编写用户注册验证的代码。以下是一个简单的例子: HTML代码: ```html <form id="registration-form"> <label for="username">Username:</label> <input type="text" id="username" name="username"> <br> <label for="password">Password:</label> <input type="password" id="password" name="password"> <br> <label for="confirm-password">Confirm Password:</label> <input type="password" id="confirm-password" name="confirm-password"> <br> <input type="submit" value="Register"> </form> ``` jQuery代码: ```javascript $(document).ready(function() { // 防止表单提交 $("#registration-form").submit(function(event) { event.preventDefault(); }); // 验证用户名 $("#username").blur(function() { var username = $(this).val(); if (!username) { alert("Username is required."); } }); // 验证密码 $("#password").blur(function() { var password = $(this).val(); if (!password) { alert("Password is required."); } }); // 验证确认密码 $("#confirm-password").blur(function() { var password = $("#password").val(); var confirmPassword = $(this).val(); if (password !== confirmPassword) { alert("Passwords do not match."); } }); // 提交表单 $("#registration-form").submit(function() { var username = $("#username").val(); var password = $("#password").val(); alert("Registration successful. Username: " + username + ", Password: " + password); }); }); ``` 这段代码将监听表单的提交事件,并防止表单默认的提交行为。然后,它将监听每个表单字段的blur事件,并验证每个字段是否符合要求。最后,如果表单验证成功,它将提交表单,并弹出一个成功消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值