国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 后端開發(fā) php教程 Ajax和PHP正則表達式驗證表單及驗證碼

Ajax和PHP正則表達式驗證表單及驗證碼

Feb 03, 2017 pm 05:48 PM

模式匹配符:

\:轉(zhuǎn)義字符 例如:\b轉(zhuǎn)義了b?

^:正則表達式開始符號?

$:正則表達式結(jié)束符號?

*:匹配前面的字符出現(xiàn)0次或者n次?

+:匹配前面的字符出現(xiàn)1次或者n次?

?:匹配前面的字符出現(xiàn)0次或者1次?

.:匹配除了換行符以外的所有單個字符?

|:或者的意思,例如x|y 匹配x或者y?

{n}:匹配前面的n個字符?

{n,m}:匹配至少n個最多m個前面字符?

[xyz]:匹配中括號里的任意一個字符?

[^xyz]:匹配除了中括號里的任意一個字符等價于[0-9]?

\w:匹配任意一個數(shù)字或字母或下劃線 等價于[A-Za-z0-9_]?

\d:匹配任意一個0--9之間的數(shù)字?

模式修正符:?

i:忽略大小寫?

常用正則表達式舉例:

//用戶名由6-18位的字母數(shù)字下劃線組成,不能由數(shù)字開頭?

var r_name=/^[a-z]\w{5,17}$/i?

//密碼長度不能少于六位?

var r_pwd=/^\w{6,}$/?

//所有的通用郵箱地址?

var r_eamil=/^\w+@\w+(\.)\w+$/?

//匹配一個QQ郵箱地址?

//861745122@qq.com?
var r_qq_email=/^\d{5,}@qq(\.)com$/?

//匹配一個163的郵箱地址?

var r_163_email=/^\w+@163(\.)com$/?

//匹配一個后綴名可能是.com|.net|.cn|.edu?

var email=/^\w+@\w+(\.)com|net|cn|edu$/?

//要求輸入有效的年齡段?

var r_age=/^\d{1,2}$/?

//if(age>=18&&age《=100)?

//驗證手機號:11位 13 15 18開頭?

var r_tel=/^1[3,5,8]\d{9}$/?

//驗證身份證號 18位或者17位加一個X?

var r_s=/^\d{18}|\d{17}x$/i?

//驗證中文 var reg=/^[\u4e00-\u9fa5]{2,17}$/?

//php?

$reg = "/^[\x{4e00}-\x{9fa5}]$/u"?

<span style="font-size:24px;">下面是一個例子:</span> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
<title></title> 
<script type="text/javascript" src="public.js"></script> 
</head> 
<body onload="yanzheng(this)"> 
<form method="post" action="info_2.php" onsubmit="return check_all()"> 
<table> 
<tr> 
<td colspan="2">賬戶基本信息</td> 
</tr> 
<tr> 
<td>登錄賬號:</td> 
<td><input type="text" name="zhanghao" onblur="check_zhanghao(this)"><span name="sp1"></span></td> 
</tr> 
<tr> 
<td>昵稱:</td> 
<td><input type="text" name="nicheng" onblur="check_nicheng(this)"><span name="sp2"></span></td> 
</tr> 
<tr> 
<td>性別:</td> 
<td><input type="radio" name="sex" value="男"onclick="check_sex()">男 
<input type="radio" name="sex" value="女"onclick="check_sex()">女 
<span id="sp3"></span></td> 
</tr> 
<tr> 
<td colspan="2">賬戶安全設(shè)置</td> 
</tr> 
<tr> 
<td>登錄密碼:</td> 
<td><input type="password" name="pwd" onblur="check_pwd(this)"><span name="sp4"></span></td> 
</tr> 
<tr> 
<td>確認(rèn)登錄密碼:</td> 
<td><input type="password" name="repwd" onblur="check_repwd(this)"><span name="sp5"></span></td> 
</tr> 
<tr> 
<td>真實姓名:</td> 
<td><input type="text" name="username" onblur="check_username(this)"><span name="sp6"></span></td> 
</tr> 
<tr> 
<td>身份證號:</td> 
<td><input type="text" name="idcard" onblur="check_idcard(this)"><span name="sp7"></span></td> 
</tr> 
<tr> 
<td>郵箱地址:</td> 
<td><input type="text" name="email" onblur="check_email(this)"><span name="sp8"></span></td> 
</tr> 
<tr> 
<td>驗證碼</td> 
<td><input type="text" id="number" onblur="check_number()"> 
<input type="button" onclick="yanzheng()" value="獲取驗證碼" > 
<span id="sp10"></span> 
<span id="sp9"></span> 
</td> 
</tr> 
<tr> 
<td></td> 
<td><input type="submit" value="免費注冊"></td> 
</tr> 
</table> 
</form> 
<script type="text/javascript"> 
//驗證登錄賬號 
function check_zhanghao(obj){ 
var sp1=$(&#39;sp1&#39;); 
if(obj.value==&#39;&#39;){ 
sp1.innerHTML=&#39;登錄賬號不能為空&#39;; 
sp1.style.color=&#39;red&#39;; 
return false; 
}else{ 
var reg=/^\w{5,10}$/i; 
if(reg.test(obj.value)){ 
sp1.innerHTML=&#39;正確&#39;; 
sp1.style.color=&#39;green&#39;; 
return true; 
}else{ 
sp1.innerHTML=&#39;登錄賬號5-10字符&#39;; 
sp1.style.color=&#39;red&#39;; 
return false; 
} 
}return true; 
} 
//驗證昵稱 
function check_nicheng(obj){ 
var sp2=$(&#39;sp2&#39;); 
if(obj.value==&#39;&#39;){ 
sp2.innerHTML=&#39;登錄賬號不能為空&#39;; 
sp2.style.color=&#39;red&#39;; 
return false; 
}else{ 
var reg=/^\w{5,10}$/i; 
if(reg.test(obj.value)){ 
sp2.innerHTML=&#39;正確&#39;; 
sp2.style.color=&#39;green&#39;; 
return true; 
}else{ 
sp2.innerHTML=&#39;昵稱5-10字符&#39;; 
sp2.style.color=&#39;red&#39;; 
return false; 
} 
}return true; 
} 
//驗證密碼 
function check_pwd(obj2){ 
var sp4=$(&#39;sp4&#39;); 
if(obj2.value==&#39;&#39;){ 
sp4.innerHTML=&#39;密碼不能為空&#39;; 
sp4.style.color=&#39;red&#39;; 
return false; 
}else{ 
var reg=/^\w{6,}$/; 
if(reg.test(obj2.value)){ 
sp4.innerHTML=&#39;正確&#39;; 
sp4.style.color=&#39;green&#39;; 
return true; 
}else{ 
sp4.innerHTML=&#39;格式不正確&#39;; 
sp4.style.color=&#39;red&#39;; 
return false; 
} 
}return true; 
} 
//驗證確認(rèn)密碼 
function check_repwd(obj3){ 
var sp5=$(&#39;sp5&#39;); 
var pwd=$(&#39;pwd&#39;); 
var repwd=$(&#39;repwd&#39;); 
if(obj3.value==&#39;&#39;){ 
sp5.innerHTML=&#39;密碼不能為空&#39;; 
sp5.style.color=&#39;red&#39;; 
return false; 
}else{ 
if(obj3.value==pwd.value){ 
sp5.innerHTML=&#39;正確&#39;; 
sp5.style.color=&#39;green&#39;; 
return true; 
}else{ 
sp5.innerHTML=&#39;確認(rèn)密碼和密碼不一致&#39;; 
sp5.style.color=&#39;red&#39;; 
return false; 
} 
}return true; 
} 
//驗證性別 
num2=0; 
function check_sex(){ 
var sex=document.getElementsByName(&#39;sex&#39;); 
// var sp4=document.getElementById(&#39;sp4&#39;) 
for(var i=0;i<sex.length;i++){ 
if(sex[i].checked==true){ 
num2=num2+1; 
} 
} 
//alert(num2); 
if(num2!=0){ 
sp3.innerHTML=&#39;√&#39;; 
sp3.style.color=&#39;green&#39;; 
return true; 
}else{ 
sp3.innerHTML=&#39;性別不能為空&#39;; 
sp3.style.color=&#39;red&#39;; 
return false; 
} 
} 
//驗證姓名 
function check_username(obj){ 
var sp6=$(&#39;sp6&#39;); 
if(obj.value==""){ 
sp6.innerHTML=&#39;用戶名不能為空&#39;; 
sp6.style.color=&#39;red&#39;; 
return false; 
}else{ 
var reg=/^[\u4e00-\u9fa5]{2,3}$/; 
if(!reg.test(obj.value)){ 
sp6.innerHTML=&#39;用戶名應(yīng)該2-3個漢字&#39;; 
sp6.style.color=&#39;red&#39;; 
return false; 
}else{ 
sp6.innerHTML=&#39;√&#39;; 
sp6.style.color=&#39;green&#39;; 
return true; 
} 
} 
return true; 
} 
//驗證郵箱 
function check_email(obj5){ 
var sp8=$(&#39;sp8&#39;); 
if(obj5.value==&#39;&#39;){ 
sp8.innerHTML=&#39;郵箱不能為空&#39;; 
sp8.style.color=&#39;red&#39;; 
return false; 
}else{ 
var reg=/^(\w+@\w+(\.)com|net|cn)$/; 
if(reg.test(obj5.value)){ 
sp8.innerHTML=&#39;正確&#39;; 
sp8.style.color=&#39;green&#39;; 
return true; 
}else{ 
sp8.innerHTML=&#39;格式不正確&#39;; 
sp8.style.color=&#39;red&#39;; 
return false; 
}return true; 
} 
} 
//驗證身份證號 
function check_idcard(obj9){ 
var sp7=$(&#39;sp7&#39;); 
if(obj9.value==&#39;&#39;){ 
sp7.innerHTML=&#39;身份證號不能為空&#39;; 
sp7.style.color=&#39;red&#39;; 
return false; 
}else{ 
var reg=/^\d{18}|\d{17}x$/i; 
if(reg.test(obj9.value)){ 
sp7.innerHTML=&#39;正確&#39;; 
sp7.style.color=&#39;green&#39;; 
return true; 
}else{ 
sp7.innerHTML=&#39;格式不正確&#39;; 
sp7.style.color=&#39;red&#39;; 
return false; 
}return true; 
} 
} 
//生成驗證碼 
function yanzheng(){ 
var sp9=document.getElementById(&#39;sp9&#39;); 
var str1=""; 
for(var i=1;i<=4;i++){ 
str1=str1+parseInt(Math.random()*10); 
sp9.innerHTML=str1; 
} 
} 
//驗證驗證碼 
function check_number(){ 
var number=document.getElementById(&#39;number&#39;).value 
var sp10=document.getElementById(&#39;sp10&#39;) 
var sp9=document.getElementById(&#39;sp9&#39;); 
if(number==""){ 
sp10.innerHTML=&#39;驗證碼不能為空&#39;; 
sp10.style.color=&#39;red&#39;; 
return false; 
}else{ 
if(number!=sp9.innerHTML){ 
sp10.innerHTML=&#39;驗證碼和你寫的不一致&#39;; 
sp10.style.color=&#39;red&#39;; 
return false; 
} else{ 
sp10.innerHTML=&#39;√&#39;; 
sp10.style.color=&#39;green&#39;; 
return true;} 
return true; 
} 
} 
function check_all(){ 
if(check_zhanghao($(&#39;zhanghao&#39;)) & check_nicheng($(&#39;nicheng&#39;)) & check_pwd($(&#39;pwd&#39;)) & check_repwd($(&#39;repwd&#39;)) & check_sex()& check_username($(&#39;username&#39;)) & check_idcard($(&#39;idcard&#39;)) &check_email($(&#39;email&#39;)) & check_number() ){ 
return true;} 
else{ return false;} 
} 
</script> 
</body> 
</html>

php正則驗證

<?php 
header("content-type:text/html;charset=utf8"); 
//var_dump($_POST);die; 
//array(5) { ["uname"]=> string(9) "劉偉超" ["uqq"]=> string(10) "1111111111" ["uemail"]=> string(12) "66555@qq.com" ["utel"]=> string(11) "15863162320" ["uinfo"]=> string(48) "地方開始放假開放活動健康的話概括" } 
empty($_POST["uname"])?$uname="":$uname=$_POST["uname"]; 
empty($_POST["uemail"])?$uemail="":$uemail=$_POST["uemail"]; 
empty($_POST["utel"])?$utel="":$utel=$_POST["utel"]; 
empty($_POST["uqq"])?$uqq="":$uqq=$_POST["uqq"]; 
empty($_POST["uinfo"])?$uinfo="":$uinfo=$_POST["uinfo"]; 
//驗證姓名 
$reg="/^[\x{4e00}-\x{9fa5}]{2,3}$/u"; 
if(!preg_match($reg,$uname)){ 
echo "用戶名應(yīng)該2-3個漢字";die; 
//header("refresh:1;url=form.html"); 
} 
//驗證郵箱 
$reg="/^(\w+@\w+(\.)com|net|cn)$/"; 
if(!preg_match($reg,$uemail)){ 
echo "郵箱必須含有@,且以com結(jié)尾";header("refresh:1;url=form.html"); die; 
} 
//驗證座機號 
$reg="/^\d{11}$/"; 
if(!preg_match($reg,$utel)){ 
echo "座機號以010-22222222格式";header("refresh:1;url=form.html"); die; 
} 
//驗證QQ號 
$reg="/^\d{5,11}$/"; 
if(!preg_match($reg,$uqq)){ 
echo "qq必須是5-11位純數(shù)字";header("refresh:1;url=form.html"); die; 
} 
//驗證簡介 
/*$reg="/^[\x{4e00}-\x{9fa5}]{10,100}\W+/u"; 
if(!preg_match($reg,$uinfo)){ 
echo "簡介應(yīng)該10-100個漢字";die; 
//header("refresh:1;url=form.html"); 
} 
*/
//連接數(shù)據(jù)庫 
$link=mysql_connect(&#39;127.0.0.1&#39;,&#39;root&#39;,&#39;root&#39;)or die("連接失敗"); 
//選擇數(shù)據(jù)庫 
mysql_select_db(&#39;kaoshi&#39;,$link); 
//設(shè)置字符集 
mysql_query("set names utf8"); 
//寫sql語句 
$sql="insert into zhuce(c_name,c_qq,c_email,c_tel,c_info) values(&#39;$uname&#39;,&#39;$uqq&#39;,&#39;$uemail&#39;,&#39;$utel&#39;,&#39;$uinfo&#39;)"; 
//echo $sql;die; 
$rel=mysql_query($sql); 
if($rel){ 
echo "注冊成功";header("refresh:1;url=show.php"); 
}else{echo "注冊失敗";header("refresh:1;url=form.html");} 
?>

以上所述是小編給大家介紹的Ajax和PHP正則表達式驗證表單及驗證碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對PHP中文網(wǎng)的支持!

更多Ajax和PHP正則表達式驗證表單及驗證碼相關(guān)文章請關(guān)注PHP中文網(wǎng)!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻,版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

對基于PHP的API進行版本控制的最佳實踐是什么? 對基于PHP的API進行版本控制的最佳實踐是什么? Jun 14, 2025 am 12:27 AM

基于toversionaphp,useUrl deuseUrl specteringforclarityAndEsofRouting,單獨的codetoavoidConflicts,dremecateOldVersionswithClearCommunication,andConsiderCustomHeadeSerlySerallyWhennEnncelsy.startbyplacingtheversionIntheUrl(E.G.,epi/api/v

如何在PHP中實施身份驗證和授權(quán)? 如何在PHP中實施身份驗證和授權(quán)? Jun 20, 2025 am 01:03 AM

tosecurelyhandleauthenticationandationallizationInphp,lofterTheSesteps:1.AlwaysHashPasswordSwithPassword_hash()andverifyusingspasspassword_verify(),usepreparedStatatementStopreventsqlineptions,andStoreSeruserDatain usseruserDatain $ _sessiveferterlogin.2.implementrole-2.imaccessccsccccccccccccccccccccccccc.

PHP中的程序和面向?qū)ο蟮木幊谭独g有什么區(qū)別? PHP中的程序和面向?qū)ο蟮木幊谭独g有什么區(qū)別? Jun 14, 2025 am 12:25 AM

procemal and object-tiriendedprogromming(oop)inphpdiffersimplessintustructure,可重復(fù)使用性和datahandling.1.procedural-Progrogursmingusesfunctimesfunctionsormanized sequalized sequalized sequiential,poiperforsmallscripts.2.OpporganizesCodeOrganizescodeOdeIntsocloceSandObjects,ModelingReal-Worlden-Worlden

PHP中有哪些弱參考(弱圖),何時有用? PHP中有哪些弱參考(弱圖),何時有用? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

如何在PHP中安全地處理文件上傳? 如何在PHP中安全地處理文件上傳? Jun 19, 2025 am 01:05 AM

要安全處理PHP中的文件上傳,核心在于驗證文件類型、重命名文件并限制權(quán)限。1.使用finfo_file()檢查真實MIME類型,僅允許特定類型如image/jpeg;2.用uniqid()生成隨機文件名,存儲至非Web根目錄;3.通過php.ini和HTML表單限制文件大小,設(shè)置目錄權(quán)限為0755;4.使用ClamAV掃描惡意軟件,增強安全性。這些步驟有效防止安全漏洞,確保文件上傳過程安全可靠。

如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進行交互? 如何與PHP的NOSQL數(shù)據(jù)庫(例如MongoDB,Redis)進行交互? Jun 19, 2025 am 01:07 AM

是的,PHP可以通過特定擴展或庫與MongoDB和Redis等NoSQL數(shù)據(jù)庫交互。首先,使用MongoDBPHP驅(qū)動(通過PECL或Composer安裝)創(chuàng)建客戶端實例并操作數(shù)據(jù)庫及集合,支持插入、查詢、聚合等操作;其次,使用Predis庫或phpredis擴展連接Redis,執(zhí)行鍵值設(shè)置與獲取,推薦phpredis用于高性能場景,Predis則便于快速部署;兩者均適用于生產(chǎn)環(huán)境且文檔完善。

PHP中==(松散比較)和===(嚴(yán)格的比較)之間有什么區(qū)別? PHP中==(松散比較)和===(嚴(yán)格的比較)之間有什么區(qū)別? Jun 19, 2025 am 01:07 AM

在PHP中,==與===的主要區(qū)別在于類型檢查的嚴(yán)格程度。==在比較前會進行類型轉(zhuǎn)換,例如5=="5"返回true,而===要求值和類型都相同才會返回true,例如5==="5"返回false。使用場景上,===更安全應(yīng)優(yōu)先使用,==僅在需要類型轉(zhuǎn)換時使用。

如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? 如何在PHP( - , *, /,%)中執(zhí)行算術(shù)操作? Jun 19, 2025 pm 05:13 PM

PHP中使用基本數(shù)學(xué)運算的方法如下:1.加法用 號,支持整數(shù)和浮點數(shù),也可用于變量,字符串?dāng)?shù)字會自動轉(zhuǎn)換但不推薦依賴;2.減法用-號,變量同理,類型轉(zhuǎn)換同樣適用;3.乘法用*號,適用于數(shù)字及類似字符串;4.除法用/號,需避免除以零,并注意結(jié)果可能是浮點數(shù);5.取模用%號,可用于判斷奇偶數(shù),處理負(fù)數(shù)時余數(shù)符號與被除數(shù)一致。正確使用這些運算符的關(guān)鍵在于確保數(shù)據(jù)類型清晰并處理好邊界情況。

See all articles