其他分享
首页 > 其他分享> > js用户名和密码合法校验

js用户名和密码合法校验

作者:互联网

<input type="text" id="userName"><br>
<input type="text" id="userPassword"><br>
<input type="button" id="btnLogin" value="登录">
.highLight {
	background-color: red;
}
// 检测用户名和密码 3-6 6-8位  不符合条件时背景高亮 都符合条件时执行登录
// 1. 获取元素
var userName = document.getElementById('userName');
var userPassword = document.getElementById('userPassword');
var btnLogin = document.getElementById('btnLogin');
// 2. 注册事件
btnLogin.onclick = function () {
	// 2.1 检查用户名和密码的长度
	// console.log(userName.value);
	if (userName.value.length < 3 || userName.value.length > 6) {
		// 2.2 不符合条件时背景高亮
		userName.className = 'highLight';
		return; // 不符合条件时不执行登录
	} else {
		userName.className = '';
	}
	if (userPassword.value.length < 6 || userPassword.value.length > 8) {
		// 2.2 不符合条件时背景高亮
		userPassword.className = 'highLight';
		return; // 不符合条件时不执行登录
	} else {
		userPassword.className = '';
	}
	console.log('执行登录');
}

在这里插入图片描述

标签:userName,用户名,userPassword,校验,value,js,className,length,符合条件
来源: https://blog.csdn.net/u010234868/article/details/117839416