5、身份證驗證:包括15位和18位。 function idNumber(s)//身份證驗證
{
regu1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;//15wei
regu2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;//18wei
re1=new RegExp(regu1);
re2=new RegExp(regu2);
if(!(re1.test(s)||re2.test(s)))
{
document.getElementById ("idCardInf").innerHTML="格式不對!";
document.getElementById ("idCardInf").style.color="red";
}
else
{
document.getElementById ("idCardInf").innerHTML="通過!";
document.getElementById ("idCardInf").style.color="green";
document.getElementById ("idCardCheck").innerHTML="";
}
// alert("請正確填寫18位或15位身份證號碼");
}
6、手機號碼驗證:13和15開頭的號碼。
function isMobileNO(s)//手機驗證
{
var a = /^((\(\d{3}\))|(\d{3}\-))?13\d{9}|15\d{9}$/ ;
if( !s.match(a) )
{
document.getElementById ("mobileNOInf").innerHTML="格式不對!";
document.getElementById ("mobileNOInf").style.color="red";
// alert("手機號碼格式不對");
}
else
{
document.getElementById ("mobileNOInf").innerHTML="通過!";
document.getElementById ("mobileNOInf").style.color="green";
document.getElementById ("mobileNOCheck").innerHTML="";
}
}
7、各表單域提示信息,獲得焦點驗證。
function displayInform(s)//注冊信息規則提示信息(獲得焦點驗證)
{
if(s=="user")
document.getElementById ("userIDCheck").innerHTML="(用戶名由4-16個數字或字母組成)";
if(s=="password")
document.getElementById ("passwordCheck").innerHTML="(密碼由6-16個數字、字母、下劃線組成,首字母必須是字母,不區分大小寫)";
if(s=="passwordC")
document.getElementById ("confirmCheck").innerHTML="(兩次密碼輸入必須一致)";
if(s=="idCard")
document.getElementById ("idCardCheck").innerHTML="(請輸入正確的號碼,以便你更容易找回密碼)";
if(s=="mobileNO")
document.getElementById ("mobileNOCheck").innerHTML="(請輸入正確的號碼,以便你更快找回密碼)";
if(s=="email")
document.getElementById ("emailCheck").innerHTML="(請輸入正確的郵箱地址,以便你更容易找回密碼)";
}
8、表單提交驗證:先對必填項進行非空驗證,再進行匹配驗證。
function submitValidator() //提交驗證
{
if(document.getElementById("control").style.display=="inline")
{
if((document.getElementById ("user").value=="")||(document.getElementById("password").value=="")||(document.getElementById("passwordC").value=="")||(document.getElementById("answer").value=="")||(document.getElementById("question").value==""))
{
alert("必填項不能為空!");
return false;
}
else
{
if((document.getElementById ("user").value).match(/^[0-9a-zA-Z]{4,16}$/)&&(document.getElementById("password").value).match(/^[a-zA-Z][0-9a-zA-Z_]{5,15}$/)&&
(document.getElementById ("password").value.toLowerCase()==document.getElementById ("passwordC").value.toLowerCase()))
{return true;}
else
{alert("輸入格式不對");return false;}
}
}
else
{
if((document.getElementById ("user").value=="")||(document.getElementById("password").value=="")||(document.getElementById("passwordC").value=="")||(document.getElementById("answer").value==""))
{
alert("必填項不能為空!");
return false;
}
else
{
if((document.getElementById ("user").value).match(/^[0-9a-zA-Z]{4,16}$/)&&(document.getElementById("password").value).match(/^[a-zA-Z][0-9a-zA-Z_]{5,15}$/)&&
(document.getElementById ("password").value.toLowerCase()==document.getElementById ("passwordC").value.toLowerCase()))
{return true;}
else
{alert("輸入格式不對");return false;}
}
}
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/diershi/archive/2009/04/16/4084981.aspx
JS表單驗證
<script language="JavaScript">
/*
*--------------- 客戶端表單通用驗證CheckForm(oForm) -----------------
* 功能:通用驗證所有的表單元素.
* 使用:
* <form name="form1" onsubmit="return CheckForm(this)">
* <input type="text" name="id" check="^\S+$" warning="id不能為空,且不能含有空格"/>
* <input type="submit"/>
* </form>
* author:wanghr100(灰豆寶寶.net)
* email:wanghr100@126.com
* update:19:28 2004-8-23
* 注意:寫正則表達式時一定要小心.不要讓"有心人"有空子鉆.
* 已實現功能:
* 對text,password,hidden,file,textarea,select,radio,checkbox進行合法性驗證
* 待實現功能:把正則表式寫成個庫.
*--------------- 客戶端表單通用驗證CheckForm(oForm) -----------------
*/
////////////////////////////////////////////////////////////////////////////////
//主函數
function CheckForm(oForm)
{
var els = oForm.elements;
//遍歷所有表元素
for(var i=0;i<els .length;i++)
{
//是否需要驗證
if(els.check)
{
//取得驗證的正則字符串
var sReg = els.check;
//取得表單的值,用通用取值函數
var sVal = GetValue(els);
//字符串->正則表達式,不區分大小寫
var reg = new RegExp(sReg,"i");
if(!reg.test(sVal))
{
//驗證不通過,彈出提示warning
alert(els.warning);
//該表單元素取得焦點,用通用返回函數
GoBack(els)
return false;
}
}
}
}
//通用取值函數分三類進行取值
//文本輸入框,直接取值el.value
//單多選,遍歷所有選項取得被選中的個數返回結果"00"表示選中兩個
//單多下拉菜單,遍歷所有選項取得被選中的個數返回結果"0"表示選中一個
function GetValue(el)
{
//取得表單元素的類型
var sType = el.type;
switch(sType)
{
case "text":
case "hidden":
case "password":
case "file":
case "textarea": return el.value;
case "checkbox":
case "radio": return GetValueChoose(el);
case "select-one":
case "select-multiple": return GetValueSel(el);
}
//取得radio,checkbox的選中數,用"0"來表示選中的個數,我們寫正則的時候就可以通過0{1,}來表示選中個數
function GetValueChoose(el)
{
var sValue = "";
//取得第一個元素的name,搜索這個元素組
var tmpels = document.getElementsByName(el.name);
for(var i=0;i<tmpels .length;i++)
{
if(tmpels.checked)
{
sValue += "0";
}
}
return sValue;
}
//取得select的選中數,用"0"來表示選中的個數,我們寫正則的時候就可以通過0{1,}來表示選中個數
function GetValueSel(el)
{
var sValue = "";
for(var i=0;i<el.options.length;i++)
{
//單選下拉框提示選項設置為value=""
if(el.options.selected && el.options.value!="")
{
sValue += "0";
}
}
return sValue;
}
}
//通用返回函數,驗證沒通過返回的效果.分三類進行取值
//文本輸入框,光標定位在文本輸入框的末尾
//單多選,第一選項取得焦點
//單多下拉菜單,取得焦點
function GoBack(el)
{
//取得表單元素的類型
var sType = el.type;
switch(sType)
{
case "text":
case "hidden":
case "password":
case "file":
case "textarea": el.focus();var rng = el.createTextRange(); rng.collapse(false); rng.select();
case "checkbox":
case "radio": var els = document.getElementsByName(el.name);els[0].focus();
case "select-one":
case "select-multiple":el.focus();
}
}
</script>
通用表單函數測試:
<form name="form1" onsubmit="return CheckForm(this)">
test:<input type="text" name="test"/>不驗證<br />
賬號:<input type="text" check="^\S+$" warning="賬號不能為空,且不能含有空格" name="id"/>不能為空<br />
密碼:<input type="password" check="\S{6,}" warning="密碼六位以上" name="id"/>六位以上<br />
電話:<input type="text" check="^\d+$" warning="電話號碼含有非法字符" name="number" value=""/><br />
相片上傳:<input type="file" check="(.*)(\.jpg|\.bmp)$" warning="相片應該為JPG,BMP格式的" name="pic" value="1"/><br />
出生日期:<input type="text" check="^\d{4}\-\d{1,2}-\d{1,2}$" warning="日期格式2004-08-10" name="dt" value=""/>日期格式2004-08-10<br />
省份:
<select name="sel" check="^0$" warning="請選擇所在省份">
<option value="">請選擇
</option><option value="1">福建省
</option><option value="2">湖北省
</option></select>
<br />
選擇你喜歡的運動:<br />
游泳<input type="checkbox" name="c" check="^0{2,}$" warning="請選擇2項或以上"/>
籃球<input type="checkbox" name="c"/>
足球<input type="checkbox" name="c"/>
排球<input type="checkbox" name="c"/>
<br />
你的學歷:
大學<input type="radio" name="r" check="^0$" warning="請選擇一項學歷"/>
中學<input type="radio" name="r"/>
小學<input type="radio" name="r"/>
<br />
個人介紹:
<textarea name="txts" check="^[\s|\S]{20,}$" warning="個人介紹不能為空,且不少于20字"></textarea>20個字以上
<input type="submit"/>
</form>
---------------------------------------------
Javascript表單編程
對form元素進行腳本編寫
獲取表單的引用
var oForm = document.forms[0]; //得到第一個表單
var oOtherForm = document.forms["formz"] //得到名為formz的表單
訪問表單字段
var oTextbox1 = oForm.textbox1; //得到名為"textbox"的字段
var oTextbox1 = oForm["text box 1"] //得到名為"text box 1"的字段
表單字段共性
var oField 1 = oForm.elements[0];
oField1.focus(); //設置焦點到第二個字段
獲得焦點:document.forms[0].name1.focus();
表單提交
使用圖片進行提交
<input type="image" src="submit.gif" />
使用submit進行提交
<input type="submit" value="submit" />
表單重置
<input type="button" value="Reset" onclick="document.forms[0].reset()">
對文本框進行腳本編寫
獲取/更改文本框的值
選擇文本
<input type="button" value="選擇文本" onclick="selectText()">
文本框事件
<input type="text" name="textbox1" value="" onselect="alert('select')" />
自動選擇文本
<input type="text" onfocus="this.select()" />
<textarea onfocus="this.select()"></textarea>
對列表框和組合框進行腳本編寫
訪問選項
獲取/更改選中項
添加選項
var ListUtil = new Object();
ListUtil.add = function(oListbox, sName, sValue){option.appendChild(document.createTextNode(sName));}
刪除選項
var oListbox = document.getElementsById("selListbox");
oListbox.remove(0); //移除一個選項
移動選項
重新排序選項
對復選框和單選框進行腳本編寫
得到單選框的值
<input type="radio" id="male" name="gender" value="male">男</input>
document.getElementById("male").value
得到復選框的值
表單驗證
使用submit事件在錯誤發生之后捕獲錯誤
使用change事件在錯誤發生時捕獲
使用keypress事件在錯誤發生之前捕獲錯誤
表單效驗最佳實踐
必須對用戶有幫助
不要讓人討厭
只要有可能,就用HTML功能代替javascript
一次顯示所有錯誤
早點捕獲錯誤
如果拿不準,就不要太嚴格
------------------------------------------------代碼實例:
<script type="text/javascript">
//function validate(){
// var user = document.getElementById("user");
// user.disabled="true"; //表示不可用
// user.focus(); //得到焦點
// onfocus="validate();" //當某個文本框得到焦點時觸發
// onblur="validate()" //當某個文本框失去焦點時觸發
///}
//獲取表單的四種方式
//function validate(){
//var form1 = document.getElementById("form1");
//var form = document.forms[0];
//var form = document.forms["form1"];
//var form = document.form1;
//alert(form==form1)
//獲取表單中的元素
//var a = form1.user;
//var a = form1.elements[0];
//var a = form1.elements["user"];
//alert(a.value)
//表單提交
//form1.submit();
//}
//獲取單選框的值
// function validate(){
// var form1 = document.getElementById("form1");
//var user = form1.user;
//user.focus();
//user.select(); //選中文本框的內容
//var rName = document.getElementsByName("radioName");
// var a = form1.radioName;
// for(var i=0;i<a.length;i++){
// if(a[i].checked){
// alert(a[i].value)
// }
// }
//}
//獲取復選框的值
/** function validate(){
var form1 = document.getElementById("form1");
var check = form1.checkName;
for(var i=0;i<check.length;i++){
if(check[i].checked){
alert(check[i].value)
}
}
}**/
function validate(){
}
//onchange事件
function chage(){
var user = document.getElementById("user");
if(user.value==""){
document.getElementById("span1").innerHTML="用戶名不能為null";
user.focus();
return false;
}else{
document.getElementById("span1").innerHTML="用戶名合法";
return ture;
}
}
</script>
<img name="img1" src="C:\Documents and Settings\黃\桌面\1.jpg" width="200px" height="200px">
<select onchange="document.img1.src=options[selectedIndex].value">
<option value="C:\Documents and Settings\黃\桌面\ff.gif">敬敬</option>
<option value="C:\Documents and Settings\黃\桌面\ff.gif">敬敬</option>
<option value="C:\Documents and Settings\黃\桌面\1.jpg">敬敬</option>
</select>
Document對象在檢驗表單中的作用
在制作登錄表單驗證的過程中,我們經常要用到Document函數對象,這是JavaScript非常重要的函數。W3C把它納入DOM文檔模型對象中。可以說它是元素操作的一個十分重要的方法。
此函數的語法代碼如下:
Document.ement.chosts.x //x可表示為屬性也可表示為方法。
如要表示表單reg中名字輸入框name的文本內容:document.reg.name.value
要讓輸入框獲得焦點:代碼格式為document.reg.name.focus()
下面是一段典型的驗證輸入函數代碼:
<script language="javascript">
function chekname(){
name=document.reg.name.value;
if(name==""){
alert("請輸入昵稱!");
document.reg.name.vlaue=focus(); 或document.reg.name.focus() ;
return false;
}else{
open("chkname.php?name="+name+"","chk","noscrollbars=no,width=200,height=50,top=200,left=200");
}
}
</script>
此段自定義函數的意思是驗證輸入框是不是空白,若不是空白則打開一個名為“chk”帶有傳遞參數的窗口進一步驗證用戶名是不是合法。
Document對象在檢驗表單中的作用

]]>