这篇文章将为大家详细讲解有关利用原生JS怎么实现一个表单验证功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
保山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!以下是JavaScript的表单验证功能,可根据JS代码编写出你想要的HTML和CSS的代码。
//1.声明变量 var emailObj; var usernameObj; var passwordObj; var confirmObj; var emailMsg; var usernameMsg; var passwordMsg; var confirmMsg; //页面加载之后,获取页面中的对象 window.onload = function() { emailObj = document.getElementById("email"); usernameObj = document.getElementById("username"); passwordObj = document.getElementById("password"); confirmObj = document.getElementById("confirm"); emailMsg = document.getElementById("emaileMsg"); usernameMsg = document.getElementById("usernameMsg"); confirmMsg = document.getElementById("confirmMsg"); }; //3.检验整个表单 function checkForm() { var bEmail = checkEmail(); var bUsername = checkUsername(); var bPassword = checkPassword(); var bConfirm = checkCondfirm(); return bUsername && bPassword && bConfirm && bEmail; } //4.验证邮箱 function checkEmail() { var regex = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/; var value = emailObj.value; var msg = ""; if(!value) { msg = "邮箱必须填写"; } else if (!regex.test(value)) { msg = "邮箱格式不合法"; } emailMsg.innerHTML = msg; emailObj.parentNode.parentNode.style.color = msg == ""?"black":"red"; return msg == ""; } //5.验证用户名‘ function checkUsername() { var regex = /^[a-zA-Z0-9_-]\w{0,9}$/; //字母数字下划线1-10位,不能是数字开头 var value = usernameObj.value; //获取usernameObj中的文本 var msg = ""; //最后的提示小事,默认为空 //如果用户名是null或"",!value的值为false,如果不为空 !value值为true if(!value) { msg = "用户名必须填写"; } else if (regex.test(value)) { msg = "用户名不合法"; } usernameMsg.innerHTML = msg; usernameObj.parentNode.parentNode.style.color = msg == ""?"black":"red"; return msg == ""; //如果提示消息为空则代表没出错,返回true } //6.验证密码 function checkPassword() { var regex = /^.{6,16}$/; //任意字符,6-16位 var value = passwordObj.value; var msg = ""; if(!value) { msg = "密码必须填写"; } else if (!regex.test(value)) { msg = "密码不合法"; } passwordMsg.parentNode.parentNode.style.color = msg == ""?"black":"red"; return msg == ""; } //7.验证确认密码 function checkCondfirm() { var passwordValue = passwordObj.value; var confirmValue = confirmObj.value; var msg = ""; if(!confirmValue) { msg = "确认密码填写"; } else if (passwordValue != confirmValue) { msg = "密码必须保持一致"; confirmMsg.innerHTML = msg; confirmObj.parentNode.parentNode.style.color = msg == ""?"black":"red"; return msg == ""; }