Web 表單驗證 javascript
////////////////////本javascript 用於驗證表單,驗證提示///////////////////////////////////////////////
////Power By WeisNet Xhtmler.Com Beta
////轉載請注時出處
////////////////////////////////2006-6-17////////////////////////////////////////////////////////////////////////////////
1.要驗證的頁面 Web.htm
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<head runat="server">
<link href="Css.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="Validate.js?ver=1.0&zWorld"></script>
<title>表單驗證</title>
</title>
<body>
<script language="javascript" type="text/javascript">...var WebCheck = new onfocusFunc();</script>
<table cellpadding="3" cellspacing="0" width="100%" style="margin:16pt auto;" id="chkFormTable">
<tr>
<td class="tdCss_f">
使用者名稱<span style="color:red">*</span>
</td>
<td align="left">
<input type="text" name="logName" id="logName" runat="server" class="inputCss" />
</td>
<td class="tdCss_t" align="left">
<div id="logName_info">2-20個字元(包括字母,數字,下劃線)!</div>
</td>
</tr>
<!------------------------------>
<tr>
<td colspan="3" class="splitTD">
</td>
</tr>
<!------------------------------>
<tr>
<td class="tdCss_f">
登陸密碼<span style="color:red">*</span>
</td>
<td align="left">
<input type="password" name="logPwd" id="logPwd" runat="server" class="inputCss" />
</td>
<td class="tdCss_t" align="left">
<div id="logPwd_info">6-20個字元(包括字母,數字,下劃線)!</div>
</td>
</tr>
<tr>
<td class="tdCss_f">
<script language="javascript" type="text/javascript">...var Web=new Main();initForm();</script>
</td>
<td align="left" colspan="2">
<input type="button" name="RegBtn" id="RegBtn" runat="server" onclick="if (!CheckAllform()) return false;" />
</td>
</tr>
</table>
</body>
</html>
<script language="javascript" type="text/javascript" src="Validate.js?ver=1.0&zWorld"></script>
<title>表單驗證</title>
</title>
<body>
<script language="javascript" type="text/javascript">...var WebCheck = new onfocusFunc();</script>
<table cellpadding="3" cellspacing="0" width="100%" style="margin:16pt auto;" id="chkFormTable">
<tr>
<td class="tdCss_f">
使用者名稱<span style="color:red">*</span>
</td>
<td align="left">
<input type="text" name="logName" id="logName" runat="server" class="inputCss" />
</td>
<td class="tdCss_t" align="left">
<div id="logName_info">2-20個字元(包括字母,數字,下劃線)!</div>
</td>
</tr>
<!------------------------------>
<tr>
<td colspan="3" class="splitTD">
</td>
</tr>
<!------------------------------>
<tr>
<td class="tdCss_f">
登陸密碼<span style="color:red">*</span>
</td>
<td align="left">
<input type="password" name="logPwd" id="logPwd" runat="server" class="inputCss" />
</td>
<td class="tdCss_t" align="left">
<div id="logPwd_info">6-20個字元(包括字母,數字,下劃線)!</div>
</td>
</tr>
<tr>
<td class="tdCss_f">
<script language="javascript" type="text/javascript">...var Web=new Main();initForm();</script>
</td>
<td align="left" colspan="2">
<input type="button" name="RegBtn" id="RegBtn" runat="server" onclick="if (!CheckAllform()) return false;" />
</td>
</tr>
</table>
</body>
</html>
2 . validate.js
///////////////////////////////////////////////////////////////////////////////////
///////ValidateForm1.0
///////Power By WeisNet Xhtmler.Com
//////////////////////////////////////////////////////////////////////////////////
var onfocusFunc = function() ...{}
var state = false;
var ComDo = new onfocusFunc();
onfocusFunc.prototype.focus = ...{
Onfocus:function(obj)
...{
var idbox = document.all.item(obj);
var idinfo;
for (var item in ComDo.Infos)
...{
if (item==obj)
...{
idinfo = ComDo.ReturnSpan(item);
idinfo.className = 'focusCss';
idinfo.innerHTML = ""+ComDo.Infos[item].b;
if (idbox.value.trim()!='')
...{
ComDo.vilidBox(idbox,item);
}
}
}
},
Onkeyup:function(obj)
...{
var idbox = document.all.item(obj);
// var idinfo = document.all.item(obj+'_info');
for (var item in ComDo.Infos)
...{
if (item==obj)
...{
idinfo = ComDo.ReturnSpan(item);
idinfo.className = 'focusCss';
//idinfo.innerHTML = ""+ComDo.Infos[item].b;
ComDo.vilidBox(idbox,item);
}
}
},
Onblur:function(obj)
...{
var idbox = document.all.item(obj);
var idinfo ;
if (idbox.value.trim()!='')
...{
for (var item in ComDo.Infos)
...{
if (item==obj)
...{
idinfo = ComDo.ReturnSpan(item);
idinfo.className = 'blurCss';
//idinfo.innerHTML = ""+ComDo.Infos[item].b;
}
}
}
else ...{
for (var item in ComDo.Infos)
...{
if (item==obj)
...{
idinfo = ComDo.ReturnSpan(item);
idinfo.className = 'blurCss';
idinfo.innerHTML = ""+ComDo.Infos[item].a;
}
}
}
}
}
onfocusFunc.prototype.Infos = ...{
logName:...{
a:"2-20個字元(包括字母,數字,下劃線)!",
b:"請填寫使用者名稱!",
c:"使用者名稱不能為空!",
d:"logName",
e:"logName_info",
f:function() ...{
ComDo.vilidBox(ComDo.ReturnTagID('logName'),'logName');
return state;
}
},
logPwd:...{
a:"6-20個字元(包括字母,數字,下劃線)!",
b:"請填寫使用者密碼!",
c:"密碼不能空!",
d:"logPwd",
e:"logPwd_info",
f:function() ...{
ComDo.vilidBox(ComDo.ReturnTagID('logPwd'),'logPwd');
return state;
}
}
}
String.prototype.trim = function()
...{
return this.replace(/(^s*)|(s*$)/g,'');
}
var initForm = function()
...{
var tbl = document.getElementById("chkFormTable");
var inputBox = tbl.getElementsByTagName("INPUT");
for (var i=0;i<inputBox.length;i++)
...{
if (inputBox[i].type=='text' || inputBox[i].type=='password')
...{
inputBox[i].onfocus = doFocus;
inputBox[i].onkeyup = doKeyup;
inputBox[i].onblur = doBlur;
}
}
}
var doFocus = function() //evnt.target Navigator
...{
var obj = event.srcElement;
ComDo.focus.Onfocus(ComDo.Infos[obj.id].d);
}
var doKeyup = function()
...{
var obj = event.srcElement;
ComDo.focus.Onkeyup(ComDo.Infos[obj.id].d);
}
var doBlur = function()
...{
var obj = event.srcElement;
ComDo.focus.Onblur(ComDo.Infos[obj.id].d);
}
//返回spn showInfo Of the box Validate
onfocusFunc.prototype.ReturnSpan = function(item)
...{
return document.all.item(ComDo.Infos[item].e);
}
onfocusFunc.prototype.ReturnTagID = function(item)
...{
return document.all.item(ComDo.Infos[item].d);
}
//驗證方法
onfocusFunc.prototype.vilidBox = function(idbox,item)
...{
idbox.value = idbox.value.trim();
var spn = ComDo.ReturnSpan(item);
switch (ComDo.Infos[item].d)
...{
case "logName":
chkLogName(idbox,spn,item);
break;
case "logPwd":
chkLogPwdf(idbox,spn,item);
break;
default:
break;
}
}
function checkByteLength(str,minlen,maxlen) ...{
if (str == null) return false;
var l = str.length;
var blen = 0;
for(i=0; i<l; i++) ...{
if ((str.charCodeAt(i) & 0xff00) != 0) ...{
blen ++;
}
blen ++;
}
if (blen > maxlen || blen < minlen) ...{
return false;
}
return true;
}
//驗證表單
function chkLogName(obj,spn,item)
...{
if (!checkByteLength(obj.value.trim(),2,20))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true;
}
}
function chkLogPwdf(obj,spn,item)
...{
var fpwd = document.all.item('chkLogPwd');
fpwd.value = '';
if (!checkByteLength(obj.value.trim(),6,20))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true;
}
}
function chkChkLogPwd(obj,spn,item)
...{
if (!checkByteLength(obj.value.trim(),6,20))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
var fpwd = document.all.item('logPwd');
if (fpwd.value.trim()!=obj.value.trim())
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].c;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true;
}
}
}
function chkLogEmail(obj,spn,item)
...{
if (!checkByteLength(obj.value.trim(),6,20))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
var RegMail = /^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]*)*@[a-zA-Z0-9-]+([.][a-zA-Z0-9-]+)+$/;
if (!RegMail.test(obj.value.trim()))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].c;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true;
}
}
}
function chkChkCode(obj,spn,item)
...{
if (obj.value.trim()=='')
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
var SecReg = /^d...{6}$/gi;
if (!SecReg.test(obj.value.trim()))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].c;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true
}
}
}
//驗證所在表單控制元件方法
onfocusFunc.prototype.CheckAllform = function()
...{
state = false;
var tbl = document.getElementById("chkFormTable");
var inputBox = tbl.getElementsByTagName("INPUT");
for (var i=0;i<inputBox.length;i++)
...{
if (inputBox[i].type=='text' || inputBox[i].type=='password')
...{
ComDo.vilidBox(inputBox[i],inputBox[i].id);
}
}
for (var k in ComDo.Infos)
...{
if (ComDo.Infos[k].f()==false)
...{
return false;
}
}
return true;
}
var state = false;
var ComDo = new onfocusFunc();
onfocusFunc.prototype.focus = ...{
Onfocus:function(obj)
...{
var idbox = document.all.item(obj);
var idinfo;
for (var item in ComDo.Infos)
...{
if (item==obj)
...{
idinfo = ComDo.ReturnSpan(item);
idinfo.className = 'focusCss';
idinfo.innerHTML = ""+ComDo.Infos[item].b;
if (idbox.value.trim()!='')
...{
ComDo.vilidBox(idbox,item);
}
}
}
},
Onkeyup:function(obj)
...{
var idbox = document.all.item(obj);
// var idinfo = document.all.item(obj+'_info');
for (var item in ComDo.Infos)
...{
if (item==obj)
...{
idinfo = ComDo.ReturnSpan(item);
idinfo.className = 'focusCss';
//idinfo.innerHTML = ""+ComDo.Infos[item].b;
ComDo.vilidBox(idbox,item);
}
}
},
Onblur:function(obj)
...{
var idbox = document.all.item(obj);
var idinfo ;
if (idbox.value.trim()!='')
...{
for (var item in ComDo.Infos)
...{
if (item==obj)
...{
idinfo = ComDo.ReturnSpan(item);
idinfo.className = 'blurCss';
//idinfo.innerHTML = ""+ComDo.Infos[item].b;
}
}
}
else ...{
for (var item in ComDo.Infos)
...{
if (item==obj)
...{
idinfo = ComDo.ReturnSpan(item);
idinfo.className = 'blurCss';
idinfo.innerHTML = ""+ComDo.Infos[item].a;
}
}
}
}
}
onfocusFunc.prototype.Infos = ...{
logName:...{
a:"2-20個字元(包括字母,數字,下劃線)!",
b:"請填寫使用者名稱!",
c:"使用者名稱不能為空!",
d:"logName",
e:"logName_info",
f:function() ...{
ComDo.vilidBox(ComDo.ReturnTagID('logName'),'logName');
return state;
}
},
logPwd:...{
a:"6-20個字元(包括字母,數字,下劃線)!",
b:"請填寫使用者密碼!",
c:"密碼不能空!",
d:"logPwd",
e:"logPwd_info",
f:function() ...{
ComDo.vilidBox(ComDo.ReturnTagID('logPwd'),'logPwd');
return state;
}
}
}
String.prototype.trim = function()
...{
return this.replace(/(^s*)|(s*$)/g,'');
}
var initForm = function()
...{
var tbl = document.getElementById("chkFormTable");
var inputBox = tbl.getElementsByTagName("INPUT");
for (var i=0;i<inputBox.length;i++)
...{
if (inputBox[i].type=='text' || inputBox[i].type=='password')
...{
inputBox[i].onfocus = doFocus;
inputBox[i].onkeyup = doKeyup;
inputBox[i].onblur = doBlur;
}
}
}
var doFocus = function() //evnt.target Navigator
...{
var obj = event.srcElement;
ComDo.focus.Onfocus(ComDo.Infos[obj.id].d);
}
var doKeyup = function()
...{
var obj = event.srcElement;
ComDo.focus.Onkeyup(ComDo.Infos[obj.id].d);
}
var doBlur = function()
...{
var obj = event.srcElement;
ComDo.focus.Onblur(ComDo.Infos[obj.id].d);
}
//返回spn showInfo Of the box Validate
onfocusFunc.prototype.ReturnSpan = function(item)
...{
return document.all.item(ComDo.Infos[item].e);
}
onfocusFunc.prototype.ReturnTagID = function(item)
...{
return document.all.item(ComDo.Infos[item].d);
}
//驗證方法
onfocusFunc.prototype.vilidBox = function(idbox,item)
...{
idbox.value = idbox.value.trim();
var spn = ComDo.ReturnSpan(item);
switch (ComDo.Infos[item].d)
...{
case "logName":
chkLogName(idbox,spn,item);
break;
case "logPwd":
chkLogPwdf(idbox,spn,item);
break;
default:
break;
}
}
function checkByteLength(str,minlen,maxlen) ...{
if (str == null) return false;
var l = str.length;
var blen = 0;
for(i=0; i<l; i++) ...{
if ((str.charCodeAt(i) & 0xff00) != 0) ...{
blen ++;
}
blen ++;
}
if (blen > maxlen || blen < minlen) ...{
return false;
}
return true;
}
//驗證表單
function chkLogName(obj,spn,item)
...{
if (!checkByteLength(obj.value.trim(),2,20))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true;
}
}
function chkLogPwdf(obj,spn,item)
...{
var fpwd = document.all.item('chkLogPwd');
fpwd.value = '';
if (!checkByteLength(obj.value.trim(),6,20))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true;
}
}
function chkChkLogPwd(obj,spn,item)
...{
if (!checkByteLength(obj.value.trim(),6,20))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
var fpwd = document.all.item('logPwd');
if (fpwd.value.trim()!=obj.value.trim())
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].c;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true;
}
}
}
function chkLogEmail(obj,spn,item)
...{
if (!checkByteLength(obj.value.trim(),6,20))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
var RegMail = /^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]*)*@[a-zA-Z0-9-]+([.][a-zA-Z0-9-]+)+$/;
if (!RegMail.test(obj.value.trim()))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].c;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true;
}
}
}
function chkChkCode(obj,spn,item)
...{
if (obj.value.trim()=='')
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].a;
state = false;
}
else
...{
var SecReg = /^d...{6}$/gi;
if (!SecReg.test(obj.value.trim()))
...{
spn.className = 'errCss';
spn.innerHTML = ComDo.Infos[item].c;
state = false;
}
else
...{
spn.className = 'focusCss';
spn.innerHTML = '輸入正確';
state = true
}
}
}
//驗證所在表單控制元件方法
onfocusFunc.prototype.CheckAllform = function()
...{
state = false;
var tbl = document.getElementById("chkFormTable");
var inputBox = tbl.getElementsByTagName("INPUT");
for (var i=0;i<inputBox.length;i++)
...{
if (inputBox[i].type=='text' || inputBox[i].type=='password')
...{
ComDo.vilidBox(inputBox[i],inputBox[i].id);
}
}
for (var k in ComDo.Infos)
...{
if (ComDo.Infos[k].f()==false)
...{
return false;
}
}
return true;
}
3. css檔案 css.css
body {...}{
}
.focusCss {...}{width:356px;height:24px;border:1px solid green;padding-Left:4pt;line-Height:24px;}
.blurCss {...}{width:356px;height:24px;border:0px solid green;padding-Left:4pt;line-Height:24px;}
.errCss {...}{width:356px;height:24px;border:1px solid red;padding-Left:4pt;line-Height:24px;color:Red;}
}
.focusCss {...}{width:356px;height:24px;border:1px solid green;padding-Left:4pt;line-Height:24px;}
.blurCss {...}{width:356px;height:24px;border:0px solid green;padding-Left:4pt;line-Height:24px;}
.errCss {...}{width:356px;height:24px;border:1px solid red;padding-Left:4pt;line-Height:24px;color:Red;}
相關文章
- javascript表單驗證JavaScript
- JavaScript 表單驗證JavaScript
- JavaScript 表單及表單驗證JavaScript
- JavaScript表單驗證事件JavaScript事件
- JavaScript表單不為空驗證JavaScript
- JavaScript 表單驗證程式碼例項JavaScript
- JavaScript 點選回車驗證提交表單JavaScript
- JavaScript------表單約束驗證DOM方法JavaScript
- bootstrap表單驗證boot
- Laravel 表單驗證Laravel
- Django表單驗證Django
- jquery 表單驗證jQuery
- Js表單驗證JS
- javascript - 使用者註冊頁面(表單驗證)JavaScript
- 前端工具-15個最佳的JavaScript表單驗證庫前端JavaScript
- 表單required 必需驗證UI
- 表單資料驗證
- HTML 表單驗證概述HTML
- bootstrapValidator 表單驗證boot
- 表單驗證——筆記筆記
- angularjs表單驗證AngularJS
- ElementUi rules表單驗證UI
- 表單驗證<AngularJs>AngularJS
- 輸入表單驗證
- Go-Web程式設計_表單_0x02_驗證表單的輸入GoWeb程式設計
- Lumen 仿 Laravel 表單驗證Laravel
- 前端表單驗證的目的前端
- 表單驗證教程簡介
- Layui 自定義表單驗證UI
- 直播平臺原始碼,JavaScript表單驗證密碼強度原始碼JavaScript密碼
- 用ASP動態生成JavaScript的表單驗證程式碼 (轉)JavaScript
- vue表單驗證你真的會了嗎?元件之表單驗證(form)validateVue元件ORM
- iview表單驗證問題 Select驗證必填失敗,以及表單物件巢狀陣列驗證方法View物件巢狀陣列
- web前端入門到實戰:css騷操作之表單驗證Web前端CSS
- elementui表單驗證 對比兩個表單大小UI
- 走進AngularJs(九)表單及表單驗證AngularJS
- Laravel 自定義表單驗證-自定義驗證規則Laravel
- Javascript使用正則驗證身份證號(簡單)JavaScript