JavaScript----正则验证用户名密码等正确

效果图 

JavaScript----正则验证用户名密码等正确_第1张图片

 js

function $(id) {
			return document.getElementById(id);
		}

		function checkUser() {
			var uname = $("uname").value;
			uname = uname.trim();
			var userp = $("unameWarn");
			userp.innerHTML = "";
			var reg = /^[a-zA-Z][a-zA-Z0-9]{5}$/;
			var reg = new RegExp("[a-zA-Z][a-zA-Z0-9]{5}");
			if (reg.test(uname) == false) {
				userp.innerHTML = "错误";
				return false;
			}
		}

		function checkPwd() {
			var psw = $("psw").value;
			psw = psw.trim();
			var pswW = $("pswWarn");
			var reg = /^[a-zA-Z0-9]{8,16}$/;
			pswW.innerHTML = "";
			if (reg.test(psw) == false) {
				pswW.innerHTML = "错误";
				return false;
			}

		}

		function checkEmail() {
			var email = $("email").value;
			email = email.trim();
			var emailW = $("emailWarn");
			var reg = /^\w+@\w+(\.[a-zA-Z]{2,4}){1,2}$/;
			emailW.innerHTML = "";
			if (reg.test(email) == false) {
				emailW.innerHTML = "错误";
				return false;
			}
		}

		function checkIp() {
			var ip = $("ip").value;
			ip = ip.trim();
			var ipW = $("ipWarn");
			var reg = /^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
			ipW.innerHTML = "";
			if (reg.test(ip) == false) {
				ipW.innerHTML = "错误";
				return false;
			}
		}

		function checktel() {
			var tel = $("tel").value;
			tel = tel.trim();
			var telW = $("telWarn");
			var reg = /^[0](\d{3})(\-)(\d){7,8}$/;
			telW.innerHTML = "";
			if (reg.test(tel) == false) {
				telW.innerHTML = "错误";
				return false;
			}
		}

html

用户名:
密码:
电子邮箱:
ip地址:
座机号码:

css

.tab {
			width: 320px;
			margin: 20px auto;
			height: 260px;
		}

		.tab tr td:first-child {
			text-align: right;
		}

		.tab tr:last-child td {
			text-align: center;
		}

你可能感兴趣的:(#JavaScript,javascript,前端,开发语言)