- 相關(guān)推薦
兩種實現(xiàn)表單驗證的javascript方法
導語:表單驗證如何實現(xiàn)呢?下面小編給大家介紹兩種實現(xiàn)表單驗證的javascript方法,大家可以參考閱讀,更多詳情請關(guān)注應屆畢業(yè)生考試網(wǎng)。
第一種:js表單驗證
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注冊-個人用戶</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script src="//cdn.bootcss.com/jquery/3.0.0-beta1/jquery.js"></script>
<style>
body {
font-family: Arial, "宋體", Lucida, Verdana, Helvetica, sans-serif;
font-size: 12px;
color: #333;
line-height: 150%;
background: #f2f2f2;
}
.hide{display:none;}
.focus,.error {
color: #e4393c;
line-height: 36px;
height: 36px;
position: absolute;
top: 0px;
width: 260px;
padding: 0 5px;
background: #FFEBEB;
border: 1px solid #ffbdbe;
}
.error span,.focus span {
padding: 5px 0;
line-height: 13px;
display: block;
}
.focus {
color: #666;
width: 260px;;
line-height: 36px;
background: #f7f7f7;
border: 1px solid #dddddd;
}
.regist {
width: 990px;
padding: 0;
margin: 0 auto;
zoom: 1;
}
.mc {
padding: 30px 0 20px;
border: solid #dddddd; border-width : 0px 1px 1px;
background: #FFF;
overflow: hidden;
zoom: 1;
border-width: 0px 1px 1px;
}
.form {
float: left;
width: 750px;
font-size: 12px;
}
.form label,.form input,.form select,.form textarea,.form button,.form .label {
float: left;
font-size: 12px;
}
.item {
padding-top: 9px;
height: 60px;
line-height: 34px;
position: relative;
z-index: 1;
}
.label {
float: left;
width: 190px;
text-align: right;
font-size: 14px;
color: #999;
padding-right: 10px;
}
.input {
float: left;
position: relative;
width: 270px;
overflow: visible;
}
.text {
float: none;
width: 275px;
height: 37px;
line-height: 32px;
border: 1px solid #cccccc;
font-size: 14px;
font-family: arial, "宋體";
overflow: hidden;
}
</style>
</head>
<body>
<p class="regist">
<p class="mc">
<form id="personRegForm" class="form" action="login.html" method="POST" onsubmit="return validateForm();">
<p class="item">
<span class="label">用戶名:</span>
<p class="input">
<input type="text" id="username" name="username" class="text">
<label id="username_msg" class="hide"></label>
</p>
</p>
<p class="item">
<span class="label">請設置密碼:</span>
<p class="input">
<input type="password" id="password" name="password" class="text">
<label id="pwd_msg" class="hide"></label>
</p>
</p>
<p class="item">
<span class="label">請確認密碼:</span>
<p class="input">
<input type="password" id="pwdRepeat" name="pwdRepeat" class="text">
<label id="pwdRepeat_msg" class="hide"></label>
</p>
</p>
<p class="item">
<span class="label">驗證郵箱:</span>
<p class="input">
<input type="text" id="mail" name="mail" class="text">
<label id="mail_msg" class="hide"></label>
</p>
</p>
<p class="item">
<span class="label"> </span>
<input type="submit" class="btn-img" id="registsubmit" value="立即注冊" />
</p>
</form>
</p>
</p>
<script>
window.onload = function(){
// 1. 用戶名
$("#username").focus(function(){
/* 獲取焦點
var username_msg = $("#username_msg");
username_msg.text("4-20位字符,支持英文、數(shù)字及'-'、'_'組合");
username_msg.attr("class","focus");
*/
elemFocus("username_msg","4-20位字符,支持英文、數(shù)字及'-'、'_'組合");
}).blur(userValidator);
// 2. 密碼
$("#password").focus(function(){
elemFocus("pwd_msg","6-20位字符,可使用字母、數(shù)字的組合");
}).blur(pwdValidator);
// 3. 確認密碼
$("#pwdRepeat").focus(function(){
elemFocus("pwdRepeat_msg","6-20位字符,可使用字母、數(shù)字的組合");
}).blur(pwdRepeatValidator);
// 4. Email
$("#mail").focus(function(){
elemFocus("mail_msg","完成驗證后,可以使用該郵箱登錄和找回密碼");
}).blur(emailValidator);
}
// 定義函數(shù) - 通用的信息提示
function elemFocus(eleId,text){
var ele_msg = $("#"+eleId);
ele_msg.text(text);
ele_msg.attr("class","focus");
}
// 定義驗證用戶名的函數(shù)
function userValidator(){
// 獲取用戶名輸入的值
var value = $("#username").val();
// 獲取用于顯示提示信息的.元素
var username_msg = $("#username_msg");
// 驗證邏輯
if(value==""||value==null){
username_msg.text("用戶名不能為空");
username_msg.attr("class","error");
return false;
}else if(value.length<4||value.length>20){
username_msg.text("用戶名的長度不正確");
username_msg.attr("class","error");
return false;
}else if(!/^[a-zA-Z0-9-_]{4,20}$/.test(value)){
username_msg.text("用戶名輸入不正確");
username_msg.attr("class","error");
return false;
}
// 驗證通過修改正確樣式
if(!username_msg.hasClass("hide")){
username_msg.text("");
username_msg.attr("class","hide");
}
return true;
}
// 定義驗證密碼的函數(shù)
function pwdValidator(){
var value = $("#password").val();
var pwd_msg = $("#pwd_msg");
if(value==""||value==null){
pwd_msg.text("密碼不能為空");
pwd_msg.attr("class","error");
return false;
}else if(value.length<6||value.length>20){
pwd_msg.text("密碼的長度不正確");
pwd_msg.attr("class","error");
return false;
}else if(!/^[a-zA-Z0-9]{6,20}$/.test(value)){
pwd_msg.text("密碼輸入不正確");
pwd_msg.attr("class","error");
return false;
}
if(!pwd_msg.hasClass("hide")){
pwd_msg.text("");
pwd_msg.attr("class","hide");
}
return true;
}
// 定義確認密碼驗證的函數(shù)
function pwdRepeatValidator(){
var value = $("#pwdRepeat").val();
var pwdRepeat_msg = $("#pwdRepeat_msg");
var pwd = $("#password").val();
if(value==""||value==null){
pwdRepeat_msg.text("密碼不能為空");
pwdRepeat_msg.attr("class","error");
return false;
}else if(value.length<6||value.length>20){
pwdRepeat_msg.text("密碼的長度不正確");
pwdRepeat_msg.attr("class","error");
return false;
}else if(!/^[a-zA-Z0-9]{6,20}$/.test(value)){
pwdRepeat_msg.text("密碼輸入不正確");
pwdRepeat_msg.attr("class","error");
return false;
}else if(value != pwd){
pwdRepeat_msg.text("兩次密碼輸入不一致");
pwdRepeat_msg.attr("class","error");
return false;
}
if(!pwdRepeat_msg.hasClass("hide")){
pwdRepeat_msg.text("");
pwdRepeat_msg.attr("class","hide");
}
return true;
}
// 定義Email驗證的函數(shù)
function emailValidator(){
var value = $("#mail").val();
var email_msg = $("#mail_msg");
if(value==""||value==null){
email_msg.text("Email不能為空");
email_msg.attr("class","error");
return false;
}else if(!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(value)){
email_msg.text("Email格式不正確");
email_msg.attr("class","error");
return false;
}
if(!email_msg.hasClass("hide")){
email_msg.text("");
email_msg.attr("class","hide");
}
return true;
}
function validateForm(){
if(!userValidator()||!pwdValidator()||!pwdRepeatValidator()||!emailValidator()){
return false;
}
return true;
}
</script>
</body>
</html>
第二種:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
* {
padding: 0;
margin: 0;
}
form {
width: 570px;
height: 300px;
margin: 100px auto;
}
label {
width: 64px;
float: left;
clear: left;
height: 36px;
line-height: 36px;
margin-top: 10px;
}
input {
width: 300px;
height: 36px;
line-height: 36px;
margin-top: 10px;
text-indent: 8px;
font-size: 16px;
font-family: "微軟雅黑";
border: 1px solid #ccc;
float: left;
}
#sub {
width: 302px;
height: 40px;
border: 1px solid #ccc;
background: #888;
color: #fff;
font-size: 18px;
text-indent: 0;
}
.spa {
height: 36px;
line-height: 36px;
width: 204px;
display: inline-block;
float: left;
font-size: 12px;
color: #BD362F;
text-indent: 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<form action="" method="post">
<label id="name">姓 名:</label><input type="text" name="username" id="username" value="" placeholder="請輸入姓名" /><span class="spa spa1"></span><br />
<label id="phone">手機號:</label><input type="text" name="userphone" id="userphone" value="" placeholder="請輸入手機號" /><span class="spa spa2"></span><br />
<label id="address">地 址:</label><input type="text" name="useraddress" id="useraddress" value="" placeholder="請輸入地址" /><span class="spa spa3"></span><br />
<label> </label><input type="submit" value="注冊" id="sub" />
</form>
<script src="http://code.jquery.com/jquery-1.4.1.js"></script>
<script type="text/javascript">
window.onload = function() {
$("#username").focus()
}
/************************ 失焦判斷 **********************************/
$("input").blur(function() {
$(".spa").css("color", "#BD362F")
if($(this).is("#username")) { //姓名判斷
var na = /^[\u4E00-\u9FA5]{2,4}$/
if($("#username").val() != "") {
if(!(na.test($("#username").val()))) {
$(".spa1").text("請輸入2-4個漢字");
$(this).css("border", "1px solid #BD362F")
return false;
} else if(na) {
$(".spa1").text("");
return true;
}
} else {
$(".spa1").text("");
}
}
if($(this).is("#userphone")) { //手機號判斷
var ph = /^1[3|5|7|8|][0-9]{9}$/
if($("#userphone").val() != "") {
if(!(ph.test($("#userphone").val()))) {
$(".spa2").text("請輸入正確手機號");
$(this).css("border", "1px solid #BD362F")
return false;
} else if(ph) {
$(".spa2").text("");
return true;
}
} else {
$(".spa2").text("");
}
}
if($(this).is("#useraddress")) { //地址判斷
var ad = /^(?=.*?[\u4E00-\u9FA5])[\dA-Za-z\u4E00-\u9FA5]{8,32}/;
if($("#useraddress").val() != "") {
if(!(ad.test($("#useraddress").val()))) {
$(".spa3").text("請輸入正確地址");
$(this).css("border", "1px solid #BD362F")
return false;
} else if(ad) {
$(".spa3").text("");
return true;
}
} else {
$(".spa3").text("");
}
}
})
/********************** 聚焦提示 ************************/
$("input").focus(function() {
if($(this).is("#username")) {
$(".spa1").text("四個漢字").css("color", "#aaa")
$(this).css("border", "1px solid #aaa")
}
if($(this).is("#userphone")) {
$(".spa2").text("11位手機號碼").css("color", "#aaa")
$(this).css("border", "1px solid #aaa")
}
if($(this).is("#useraddress")) {
$(".spa3").text("最少8個字符(漢字、字母和數(shù)字)").css("color", "#aaa")
$(this).css("border", "1px solid #aaa")
}
})
/*********************** 提交驗證 ***************************/
$("#sub").click(function() {
var na = /^[\u4E00-\u9FA5]{2,4}$/; //姓名正則
var ph = /^1[3|5|7|8|][0-9]{9}$/; //手機號正則
var ad = /^(?=.*?[\u4E00-\u9FA5])[\dA-Za-z\u4E00-\u9FA5]{8,32}/; //地址正則
if(na.test($("#username").val()) && ph.test($("#userphone").val()) && ad.test($("#useraddress").val())) {
return true;
} else {
if($("#username").val() == "") {
$(".spa1").text('請你填寫用戶名')
}
if($("#userphone").val() == "") {
$(".spa2").text('請你填寫手機號')
}
if($("#useraddress").val() == "") {
$(".spa3").text('請你填寫地址')
}
return false;
}
})
</script>
</body>
</html>
【兩種實現(xiàn)表單驗證的javascript方法】相關(guān)文章:
基于PHP+Ajax實現(xiàn)表單驗證的詳解08-22
ThinkPHP中create()方法自動驗證表單信息09-10
如何實現(xiàn)php登陸表單提交CSRF及驗證碼09-14
JavaScript如何實現(xiàn)JSON.stringify09-19
php如何實現(xiàn)驗證碼06-13
如何使用javascript實現(xiàn)瀑布流及效果加載06-17