正則表達式驗證代碼(字母、數字、Email、網址、電話號碼、漢字、身份證號碼)

 

正則表達式驗證代碼(字母、數字、Email、網址、電話號碼、漢字、身份證號碼) (轉載)

%
if request("check")<>"" then
astr=request("content")
call str(astr)
end if
function str(astr)
Ar=Cint(Asc(astr))
if(65<=Ar and Ar<=90) or (97<=Ar and Ar<=122) then
response.Write "<script>alert('輸入的字符串是英文字母!!');</script>"
else
response.write "<script>alert('輸入的字符串不是英文字母!!');</script>"
end if
end function

%>

服務器端驗證Email是否正確
<%
Function F_RegExp(Expression, IfStr)
Dim RegExp1, Matches
Set ObjExp = New RegExp '建立正則表達式
ObjExp.Pattern = Expression '設置模式
ObjExp.IgnoreCase = True '設置是否區分字符大小寫
ObjExp.Global = True '設置全局可用性
Matches = ObjExp.Test(IfStr)
F_RegExp = Matches
End Function
function checkemail(str)
checkemail=F_RegExp("\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",trim(str))
end function
%>
<%
if request.form("e_mail")<>"" then
if cbool(checkemail(request.form("e_mail"))) then
response.Write("您輸入的Email地址正確!")
else
response.Write("您輸入的Email地址錯誤!")
end if
else
response.Write("您沒有輸入Email地址!")
end if
%>

客戶端驗證Email是否正確
<script language="javascript">
function check(myform){
if(myform.e_mail.value==""){
alert("請輸入Email地址!");myform.e_mail.focus();return;
}
if(!checkemail(myform.e_mail.value)){
alert("您輸入Email地址不正確!");myform.e_mail.focus();return;
}
myform.submit();
}
function checkemail(email){
var str=email;
//在JavaScript中,正則表達式只能使用"/"開頭和結束,不能使用雙引號
var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
var objExp=new RegExp(Expression);
if(objExp.test(str)==true){
return true;
}else{
return false;
}
}
</script>

通過正則表達式驗證電話號碼
<%
Public Function F_RegExp(Expression, IfStr)
Dim RegExp1, Matches
Set ObjExp = New RegExp '建立正則表達式
ObjExp.Pattern = Expression '設置模式
ObjExp.IgnoreCase = True '設置是否區分字符大小寫
ObjExp.Global = True '設置全局可用性
Matches = ObjExp.Test(IfStr)
F_RegExp = Matches
End Function
function checktel(str)
checktel=F_RegExp("(\d{3}-)?\d{8}|(\d{4}-)(\d{7})",trim(str))
end function
%>
<%
if request.form("Tel")<>"" then
if cbool(checktel(request.form("Tel"))) then
response.Write("您輸入的電話號碼正確!")
else
response.Write("您輸入的電話號碼錯誤!")
end if
else
response.Write("您沒有輸入聯繫電話!")
end if
%>

驗證輸入的字符串是否爲漢字
<script language="javascript">
function check(myform){
if(myform.realname.value==""){
alert("請輸入真實姓名!");myform.realname.focus();return;
}
if(checkrealname(myform.realname.value)){
alert("您輸入真實姓名不正確!");myform.realname.focus();return;
}
myform.submit();
}
function checkrealname(realname){
var str=realname;
//在JavaScript中,正則表達式只能使用"/"開頭和結束,不能使用雙引號
var Expression=/[^\u4E00-\u9FA5]/;
var objExp=new RegExp(Expression);
if(objExp.test(str)==true){
return true;
}else{
return false;
}
}
</script>

驗證身份證號碼
<script language="javascript">
function check(myform){
if(myform.number.value==""){
alert("請輸入身份證號碼地址!");myform.number.focus();return;
}
if(!checkeNO(myform.number.value)){
alert("您輸入身份證號碼不正確!");myform.number.focus();return;
}
myform.submit();
}
function checkeNO(NO){
var str=NO;
//在JavaScript中,正則表達式只能使用"/"開頭和結束,不能使用雙引號
var Expression=/\d{17}[\d|X]|\d{15}/;
var objExp=new RegExp(Expression);
if(objExp.test(str)==true){
return true;
}else{
return false;
}
}
</script>

客戶端驗證用戶名和密碼
<script language="javascript">
function check(myform){
if(myform.username.value==""){
alert("請輸入用戶名!");myform.username.focus();return;
}
if(!checkeusername(myform.username.value)){
alert("您輸入的用戶名不合法!");myform.username.focus();return;
}
if(myform.PWD.value==""){
alert("請輸入密碼!");myform.PWD.focus();return;
}
if(!checkePWD(myform.PWD.value)){
alert("您輸入的密碼不合法!");myform.PWD.focus();return;
}
if(myform.PWD1.value==""){
alert("請確認密碼!");myform.PWD1.focus();return;
}
if(myform.PWD1.value!=myform.PWD.value){
alert("您兩次輸入的密碼不一致,請重新輸入!");myform.PWD.focus();return;
}
myform.submit();
}
function checkeusername(username){
var str=username;
//在JavaScript中,正則表達式只能使用"/"開頭和結束,不能使用雙引號
var Expression=/^(\w){3,10}$/;
var objExp=new RegExp(Expression);
if(objExp.test(str)==true){
return true;
}else{
return false;
}
}
function checkePWD(PWD){
var str=PWD;
//在JavaScript中,正則表達式只能使用"/"開頭和結束,不能使用雙引號
var Expression=/^[A-Za-z]{1}([A-Za-z0-9]|[._]){5,19}$/;
var objExp=new RegExp(Expression);
if(objExp.test(str)==true){
return true;
}else{
return false;
}
}
</script>

驗證網址是否合法
<script language="javascript">
function checkuserinfo(){
var homepage=userinfo.homepage.value;
if(homepage==""){
alert("請輸入個人主頁地址!");
document.userinfo.homepage.focus();
}else{
if(!checkeURL(homepage)){
alert("您輸入的個人主頁地址不合法!");
document.userinfo.homepage.focus();
return;
}
}
userinfo.submit();
}
function checkeURL(URL){
var str=URL;
//在JavaScript中,正則表達式只能使用"/"開頭和結束,不能使用雙引號
//判斷URL地址的正則表達式爲:http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
//下面的代碼中應用了轉義字符"\"輸出一個字符"/"
var Expression=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
var objExp=new RegExp(Expression);
if(objExp.test(str)==true){
return true;
}else{
return false;
}
}
</script>

驗證數量和金額
<script language="JavaScript">
function checkPrice(){
if (form1.dj.value==0 && form1.dj.value==""){
alert("請輸入單價!");form1.dj.focus();form1.dj.select();return;}
if(isNaN(form1.dj.value)){
alert("您輸入的單價不是有效值!");form1.dj.focus();form1.dj.select();return;
}
}
function checkNum(){
if (form1.sl.value==0 && form1.sl.value==""){
alert("請輸入數量!");form1.sl.focus();form1.sl.select();return;}
str=form1.sl.value;
len=str.length;
for(i=0;i<len;i++){
x=str.substr(i,1);
if(x!="1"&x!="2"&x!="3"&x!="4"&x!="5"&x!="6"&x!="7"&x!="8"&x!="9"){
alert("您輸入的不是有效值(請輸入整數)!");form1.sl.focus

();form1.sl.select();return;
}
}
}
function aotoPay(){
form1.je.value=form1.sl.value*form1.dj.value;
if (isNaN(form1.je.value)){
form1.je.value=0;
}
}
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章