js實(shí)現(xiàn)表單及時(shí)驗(yàn)證功能 用戶信息立即驗(yàn)證
問題:表單怎么在輸入后立即驗(yàn)證,而不是提交后再驗(yàn)證那么不方便(網(wǎng)上搜到的要么是模棱兩可,要么是殘缺不全…)
方法:鑒于此,小可,水山奇,將其代碼補(bǔ)全,加上小可我個(gè)人的理解(注釋)在上面,僅供后來者少走彎路,也請各路好漢批評(píng)指正…(轉(zhuǎn)發(fā)請注作者,xiexie)————table表格版,以后會(huì)繼續(xù)有JQuery版…
如果幫助到您,頂一下 ヾ(≧O≦)〃嗷~
截圖:

代碼:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用戶注冊</title>
<!-- 此處引用外部css樣式 -->
<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript">
//及時(shí)驗(yàn)證用戶名
function checkuse(){
//在每個(gè)函數(shù)中定義check變量是為了在表單提交后,能夠逐個(gè)驗(yàn)證每個(gè)函數(shù)是否通過,很好很好。(以下同理)
var check;
var username = document.getElementById("username").value;
if (username.length > 18 || username.length < 6) {
alert("用戶名輸入不合法,請重新輸入!");
//此處甚妙,既然你在此處輸入錯(cuò)誤,那么按理說當(dāng)然要在此處繼續(xù)輸入了。(在此處繼續(xù)獲取焦點(diǎn)?。?
document.getElementById("username").focus();
check = false;
} else {
document.getElementById("checktext1").innerHTML = "* 用戶名由6-18位字符組成 √";
check = true;
}
return check;
}
//利用正則表達(dá)式判斷密碼符合否
function checkpwd() {
var check;
var reg = /[^A-Za-z0-9_]+/;
var regs = /^[a-zA-Z0-9_\u4e00-\u9fa5] + $ /;
var password = document.getElementById("password").value;
if (password.length < 6 || password.length > 18 || regs.test(password)) {
alert("密碼輸入不合法,請重新輸入!");
document.getElementById("password").focus();
check = false;
} else {
document.getElementById("checktext2").innerHTML = "* 密碼由6-18位字符組成,且必須包含字母、數(shù)字和標(biāo)點(diǎn)符號(hào) √";
check = true;
}
return check;
}
//驗(yàn)證密碼是否不一致!
function checkpwdc() {
var check;
var password = document.getElementById("password").value;
var pwdc = document.getElementById("pwdc").value;
if (password != pwdc) {
alert("兩次輸入密碼不一致,請重新輸入!");
document.getElementById("pwdc").focus();
check = false;
} else {
document.getElementById("checktext3").innerHTML = "* 請?jiān)俅屋斎肽愕拿艽a √";
check = true;
}
return check;
}
//提交時(shí)驗(yàn)證用戶類別
function checkut(){
var check;
if(document.getElementById("selUser").selectedIndex == 0)
{
alert("請選擇用戶類型!");
document.getElementById("selUser").focus();
check = false;
}else{
document.getElementById("checktext4").innerHTML = "* 請選擇用戶類型 √";
check = true;
}
return check;
}
//提交時(shí)驗(yàn)證用戶性別
function checkGender(){
var check;
var gender = "";
//獲取所有名稱為sex的標(biāo)簽
var sex = document.getElementsByName("sex");
//遍歷這些名稱為sex的標(biāo)簽
for(var i=0;i<sex.length;++i){
//如果某個(gè)sex被選中,則記錄
if(sex[i].checked)
gender = sex[i].value;
}
if(gender == "")
{
alert("請選擇性別!");
check = false;
}else{
document.getElementById("checktext5").innerHTML = "* 請選擇你的性別 √";
check = true;
}
return check;
}
//及時(shí)驗(yàn)證出生日期
function checkDate(){
var check;
if(document.getElementById("txtDate").value ==""){
alert("請?zhí)顚懗錾掌冢?);
document.getElementById("txtDate").focus();
check = false;
}else{
document.getElementById("checktext6").innerHTML = "* 請選擇你的出生日期 √";
check = true;
}
return check;
}
//及時(shí)驗(yàn)證興趣愛好
function checkHobby(){
var check;
var hobby = 0;
//objNum為所有名稱為hobby的input標(biāo)簽
var objNum = document.getElementsByName("hobby");
//遍歷所有hobby標(biāo)簽
for(var i=0;i<objNum.length;++i){
//判斷某個(gè)hobby標(biāo)簽是否被選中
if(objNum[i].checked==true)
hobby++;
}
//如果有選中的hobby標(biāo)簽
if(hobby >=1){
document.getElementById("checktext7").innerHTML = "* 請選擇你的興趣愛好 √";
check = true;
}else{
alert("請?zhí)顚憪酆茫?);
check = false;
}
return check;
}
//正則表達(dá)式驗(yàn)證電子郵件(及時(shí))
function checkemail(){
var check;
//電子郵件的正則表達(dá)式
var e1 = document.getElementById("email").value.indexOf("@",0);
var e2 = document.getElementById("email").value.indexOf(".",0);
if(email == "" || (e1==-1 || e2==-1) || e2<e1 )
{
alert("E_mail輸入錯(cuò)誤!");
document.getElementById("email").focus();
check = false;
} else {
document.getElementById("checktext8").innerHTML = "* 請?zhí)顚懗S玫腅MAIL,將用于密碼找回 √";
check = true;
}
return check;
}
//及時(shí)驗(yàn)證自我介紹
function checkintro(){
var check;
var intro = document.getElementById("introduction").value;
if (intro.length > 100) {
alert("字?jǐn)?shù)超限!");
check = false;
} else {
document.getElementById("checktext9").innerHTML = "* 限100字內(nèi) √";
document.getElementById("checktext9").focus();
check = true;
}
return check;
}
//提交表單時(shí)所有都驗(yàn)證一遍(若任何一個(gè)驗(yàn)證不通過,則返回為false,阻止表單提交)
function check() {
var check = checkuse() && checkpwd() && checkpwdc() && checkut() && checkGender() && checkDate() && checkHobby()
&& checkemail() &&checkintro();
return check;
}
</script>
</head>
<body >
<!-- <form action ="跳轉(zhuǎn)頁面" method ="get"|"post" name ="表單名稱" target ="打開方式" enctype="multipart/form-data" > -->
<!-- onsubmit()函數(shù)在返回值為true時(shí)提交表單。 -->
<form action="#" method="get" onsubmit="return check()" >
<fieldset>
<legend>
表單及時(shí)驗(yàn)證小例子
</legend>
<table align="left" style="background-image: url('img/4.jpg');" >
<tr>
<td>用戶名:</td>
<td><input type="text" name="username" id="username" onchange=" checkuse()" /></td>
<td id="checktext1">* 用戶名由6-18位字符組成</td>
</tr>
<!-- onblur 事件處理程序:當(dāng)元素或窗口失去焦點(diǎn)時(shí)觸發(fā)該事件 -->
<!-- onchange事件處理程序:當(dāng)表單元素獲取焦點(diǎn),并且內(nèi)容發(fā)生改變時(shí),觸發(fā)該事件 -->
<!-- 以下同理 -->
<tr>
<td>密碼:</td>
<td><input type="password" name="password" id="password" onchange="checkpwd()" /></td>
<td id="checktext2">* 密碼由6-18位字符組成,且必須包含字母、數(shù)字和標(biāo)點(diǎn)符號(hào)</td>
</tr>
<tr>
<td>確認(rèn)密碼:</td>
<td><input type="password" name="pwdc" id="pwdc" onchange="checkpwdc()" /></td>
<td id="checktext3">* 請?jiān)俅屋斎肽愕拿艽a</td>
</tr>
<tr>
<td>用戶類型:</td>
<td>
<select id="selUser" onblur="checkut()">
<option name="selUser" value="0">請選擇</option>
<option name="selUser" value="1">管理員</option>
<option name="selUser" value="2">普通用戶</option>
</select>
</td>
<td id="checktext4">* 請選擇用戶類型</td>
</tr>
<tr>
<td>性別:</td>
<td>
<input type="radio" value="1" name="sex" onchange="checkGender()"/>男
<input type="radio" value="2" name="sex" onchange="checkGender()"/>女
</td>
<td id="checktext5">* 請選擇你的性別</td>
</tr>
<tr>
<td>出生日期:</td>
<td><input type="date" name="date" id="txtDate" onblur="checkDate()"/></td>
<td id="checktext6">* 請選擇你的出生日期</td>
</tr>
<tr>
<td>興趣愛好:</td>
<td>
<input type="checkbox" name="hobby" value="reading" onchange="checkHobby()">閱讀
<input type="checkbox" name="hobby" value="music" onchange="checkHobby()">音樂
<input type="checkbox" name="hobby" value="sports" onchange="checkHobby()">運(yùn)動(dòng)
</td>
<td id="checktext7">* 請選擇你的興趣愛好</td>
</tr>
<tr>
<td>電子郵件:</td>
<td><input type="text" name="email" id="email" onchange="checkemail()"/></td>
<td id="checktext8">* 請?zhí)顚懗S玫腅MAIL,將用于密碼找回</td>
</tr>
<tr>
<td>自我介紹:</td>
<td><textarea cols="30" rows="3" name="introduction" id="introduction" onchange="checkintro()">這是自我介紹...</textarea></td>
<td id="checktext9">* 限100字內(nèi)</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="提交" />
<input type="reset" name="reset" value="重置" />
</td>
</tr>
</table>
</fieldset>
</form>
</body>
</html>
CSS樣式:
input:focus,textarea:focus{
border:1px solid #f00;
background:#fcc;
}
textarea{
width:230px;
height:50px;
}
body
{
font-size:15px;
/* 字體的樣式 */
font-family:Microsoft YaHei;
}
select option{
font-size:10px;
font-family:Microsoft YaHei;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- js表單驗(yàn)證實(shí)例講解
- 擁有一個(gè)屬于自己的javascript表單驗(yàn)證插件
- javascript html5實(shí)現(xiàn)表單驗(yàn)證
- AngularJS自動(dòng)表單驗(yàn)證
- JS組件Form表單驗(yàn)證神器BootstrapValidator
- 詳解AngularJS實(shí)現(xiàn)表單驗(yàn)證
- 快速學(xué)習(xí)jQuery插件 jquery.validate.js表單驗(yàn)證插件使用方法
- 常用javascript表單驗(yàn)證匯總
- jquery validate.js表單驗(yàn)證入門實(shí)例(附源碼)
- js 常用正則表達(dá)式表單驗(yàn)證代碼
相關(guān)文章
把input初始值不寫value的具體實(shí)現(xiàn)方法
比如制作一個(gè)最常見的,input初始值,一般以前,我都只是寫在input的value里,要把初始值單獨(dú)寫出來,于是我比較傻逼,就用<span>標(biāo)簽寫,定位在input上,讓它單擊和input獲焦上都消失2013-07-07
JavaScript中array.reduce()數(shù)組方法的四種使用實(shí)例
reduce為數(shù)組中的每一個(gè)元素依次執(zhí)行回調(diào)函數(shù),不包括數(shù)組中被刪除或從未被賦值的元素,下面這篇文章主要給大家介紹了關(guān)于JavaScript中array.reduce()數(shù)組方法的四種使用實(shí)例,需要的朋友可以參考下2022-07-07
JS簡單實(shí)現(xiàn)仿百度控制臺(tái)輸出信息效果
這篇文章主要介紹了JS簡單實(shí)現(xiàn)仿百度控制臺(tái)輸出信息效果,涉及javascript中console.log函數(shù)的簡單使用技巧,需要的朋友可以參考下2016-09-09
用JS動(dòng)態(tài)設(shè)置CSS樣式常見方法小結(jié)(推薦)
本文給大家總結(jié)了js動(dòng)態(tài)設(shè)置css樣式的常見方法,非常實(shí)用,對(duì)js設(shè)置css樣式相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-11-11
javascript結(jié)合fileReader 實(shí)現(xiàn)上傳圖片
FileReader具體支持哪些方法和事件,這里就不介紹了,有興趣的可以去w3c官網(wǎng)上看看FileReader介紹,這里主要介紹一下FileReader常見應(yīng)用,以及fileReader 實(shí)現(xiàn)上傳圖片示例分享。2015-01-01

