下面給大家分享HTML用正則表達(dá)式檢驗(yàn)表格的實(shí)例代碼,具體代碼內(nèi)容如下所示:
<span style="font-size:24px;color:#cc6600;"> 正則表達(dá)式在JavaScript腳本中是很好用的檢驗(yàn)語法規(guī)則的方法。但是與Java中的正則表達(dá)式有所不同。它需要在regex規(guī)則上以“^”開始,以"$"結(jié)束。</span>
<span style="font-size:24px;color:#cc6600;">以下讓我們看看一個(gè)實(shí)例。</span>
<span style="font-size:18px;"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
form table tr td{
border: 1px solid lightgrey;
text-align: center;
}
form table tr td input{
width: 97%;
}
</style>
<script language="JavaScript" type="text/javascript">
//判斷規(guī)則(正則表達(dá)式)
function goto() {
var name = document.getElementById("name");
var pwd = document.getElementById("pwd");
var pwd2 = document.getElementById("pwd2");
var pnum = document.getElementById("pnum");
var phone = document.getElementById("phone");
var telephone = document.getElementById("telephone");
var email = document.getElementById("email");
if (name.value.trim().length<=8){
alert("用戶名長(zhǎng)度必須大于八位");
name.focus()
name.value="";
return;
}
// 規(guī)則必須包括大小寫字母,數(shù)字
var regex = /^(?!(?:\d+|[a-zA-Z]+|[\da-z]+|[\dA-Z])$)[\da-zA-Z]{6,}$/;
// var regex = /^[A-z0-9]{10,20}$/;
if ( !pwd.value.match(regex)){
alert("密碼不符合規(guī)定");
pwd.focus()
pwd.value="";
return;
}
if (pwd.value != pwd2.value){
alert("兩次輸入的密碼不相同");
pwd2.focus()
pwd.value="";
pwd2.value="";
return;
}
var rege=/^\d{17}X$|^\d{15}$/;
if (!rege.test(pnum.value)){
alert("身份證不符合規(guī)定");
pnum.focus()
pnum.value="";
return;
}
var regex2 = /^\d{4}-\d{7}$/;//判斷座機(jī)號(hào)
if (!regex2.test(phone.value)){
alert("座機(jī)號(hào)碼不符合規(guī)定");
phone.focus()
phone.value="";
return;
}
var regex3 = /^1[3,5,7,8]\d{9}$/;
if (!regex3.test(telephone.value)){
alert("手機(jī)號(hào)碼不符合規(guī)定");
telephone.focus()
telephone.value="";
return;
}
//test方法必須用反斜杠轉(zhuǎn)義
var regex4 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if (!regex4.test(email.value)){
alert("郵箱不符合規(guī)定");
email.focus()
email.value="";
return;
}
}
</script>
</head>
<body>
<form>
<table style="width:600px;height: 300px;border: 1px solid lightgrey">
<tr>
<td width="18%">登錄名:</td>
<td><input id="name"type="text"></td>
<td width="50%">長(zhǎng)度大于八位</td>
</tr>
<tr>
<td>登錄密碼:</td>
<td><input id="pwd"type="password"></td>
<td>長(zhǎng)度大于十位,包含字母數(shù)字</td>
</textarea></td>
</tr>
<tr>
<td>確認(rèn)密碼:</td>
<td><input id="pwd2" type="password"></td>
</tr>
<tr>
<td>身份證號(hào)碼:</td>
<td><input id="pnum" type="text"></td>
<td>15位或18位最后一個(gè)是X</td>
</tr>
<tr>
<td>固定電話:</td>
<td><input id="phone"type="text"></td>
<td>格式xxxx-xxxxxxx</td>
</tr>
<tr>
<td>手機(jī)號(hào)碼:</td>
<td><input id="telephone"type="text"></td>
<td>11位整數(shù)</td>
</tr>
<tr>
<td>電子郵件:</td>
<td><input id="email"type="text"></td>
<td>xxxx@xxx.xxx xxx@xxx.xxx.xx</td>
</tr>
<tr>
<td>現(xiàn)居住地:</td>
<td><select>
<option>--選擇省份--</option>
<option>北京</option>
<option>河北</option>
<option>廣西</option>
</select></td>
<td><select>
<option>--選擇城市--</option>
<option>煙臺(tái)</option>
<option>青島</option>
<option>哈爾濱</option>
</select></td>
</tr>
<tr>
<td colspan="1"></td>
<td><input id="submit" type="button" value="提交注冊(cè)信息" style="width: 100px" onclick="goto()"></td>
<td><input type="reset" value="重置" style="width: 60px"></td>
</tr>
</table>
</form>
</body>
</html></span>
附:一些常使用的規(guī)則:
"^\\d+$" //非負(fù)整數(shù)(正整數(shù) + 0)
"^[0-9]*[1-9][0-9]*$" //正整數(shù)
"^((-\\d+)|(0+))$" //非正整數(shù)(負(fù)整數(shù) + 0)
"^-[0-9]*[1-9][0-9]*$" //負(fù)整數(shù)
"^-?\\d+$" //整數(shù)
"^\\d+(\\.\\d+)?$" //非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0)
"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮點(diǎn)數(shù)
"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$" //非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0)
"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //負(fù)浮點(diǎn)數(shù)
"^(-?\\d+)(\\.\\d+)?$" //浮點(diǎn)數(shù)
"^[A-Za-z]+$" //由26個(gè)英文字母組成的字符串
"^[A-Z]+$" //由26個(gè)英文字母的大寫組成的字符串
"^[a-z]+$" //由26個(gè)英文字母的小寫組成的字符串
"^[A-Za-z0-9]+$" //由數(shù)字和26個(gè)英文字母組成的字符串
"^\\w+$" //由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串
"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$" //email地址
"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$" //url
以上所述是小編給大家介紹的HTML用正則表達(dá)式檢驗(yàn)表格的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!