常用js驗證

//字符長短 用戶名
function check_name()
{
    var upname= document.getElementById("txt_name").value;
    var re =new RegExp("^.{5,50}$");
    var arr;
if((arr = re.exec(upname)) == null)
{
document.getElementById("err_name").className = "style5";
document.getElementById("txt_name").value = "";
return;
}
document.getElementById("err_name").className = "style4";
return true;
}

//字符長短 密碼
function check_pwd()
{
    var upoldpwd= document.getElementById("txt_pwd").value;
    var re =new RegExp("^.{5,50}$");
    var arr;
if((arr = re.exec(upoldpwd)) == null)
{
document.getElementById("err_pwd").className = "style5";
document.getElementById("txt_pwd").value = "";
return;
}
document.getElementById("err_pwd").className = "style4";
return true;
}

//是否重複 驗證密碼
function check_two_pwd()
{
    var tit1= document.getElementById("txt_pwd").value;
    var tit2= document.getElementById("txt_check_pwd").value;

if(tit1.length == "" || tit1 != tit2)
{
document.getElementById("err_checkpwd").className = "style5";
document.getElementById("txt_pwd").value = "";
document.getElementById("txt_check_pwd").value = "";
return;
}
document.getElementById("err_checkpwd").className = "style4";
return true;
}

//E-mail 驗證mail
function check_mail()
{
var str= document.getElementById("txt_mail").value;
var sReg = /[_a-zA-Z/d/-/.]+@[_a-zA-Z/d/-]+(/.[_a-zA-Z/d/-]+)+$/;
if (!sReg.test(str))
{
document.getElementById("err_mail").className = "style5";
document.getElementById("txt_mail").value = "";
return;
}

document.getElementById("err_mail").className = "style4";
return true;
}

//是否數字 驗證桌號
function check_table()
{
var srange = document.getElementById("txt_table").value;
var patrn=/^[0-9]{1,20}$/;
if(srange == null || srange == "" || !patrn.exec(srange) || srange.indexOf('0') == 0)
{
document.getElementById("err_checktable").className = "style5";
document.getElementById("txt_table").value = "";
return false;
}

document.getElementById("err_checktable").className = "style4";
return true;
}

js驗證表單大全
1. 長度限制
<script>
function test()
{
if(document.a.b.value.length>50)
{
alert("不能超過50個字符!");
document.a.b.focus();
return false;
}
}
</script>
<form name=a οnsubmit="return test()">
<textarea name="b" cols="40" wrap="VIRTUAL" rows="6"></textarea>
<input type="submit" name="Submit" value="check">
</form>

2. 只能是漢字
<input οnkeyup="value="/oblog/value.replace(/[^/u4E00-/u9FA5]/g,'')">

3." 只能是英文
<script language=javascript>
function onlyEng()
{
if(!(event.keyCode>=65&&event.keyCode<=90))
event.returnvalue=false;
}
</script>

<input οnkeydοwn="onlyEng();">

4. 只能是數字
<script language=javascript>
function onlyNum()
{
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
//考慮小鍵盤上的數字鍵
event.returnvalue=false;
}
</script>

<input οnkeydοwn="onlyNum();">

5. 只能是英文字符和數字
<input οnkeyup="value="/oblog/value.replace(/[/W]/g,"'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/d]/g,''))">

6. 驗證油箱格式
<SCRIPT LANGUAGE=javascript RUNAT=Server>
function isEmail(strEmail) {
if (strEmail.search(/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/) != -1)
return true;
else
alert("oh");
}
</SCRIPT>
<input type=text οnblur=isEmail(this.value)>

7. 屏蔽關鍵字(這裏屏蔽***和****)
<script language="javascript1.2">
function test() {
if((a.b.value.indexOf ("***") == 0)||(a.b.value.indexOf ("****") == 0)){
alert(":)");
a.b.focus();
return false;}
}
</script>
<form name=a οnsubmit="return test()">
<input type=text name=b>
<input type="submit" name="Submit" value="check">
</form>

8. 兩次輸入密碼是否相同
<FORM METHOD=POST ACTION="">
<input type="password" id="input1">
<input type="password" id="input2">
<input type="button" value="test" οnclick="check()">
</FORM>
<script>
function check()
{
with(document.all){
if(input1.value!=input2.value)
{
alert("false")
input1.value = "";
input2.value = "";
}
else document.forms[0].submit();
}
}
</script>
夠了吧
屏蔽右鍵 很酷
οncοntextmenu="return false" οndragstart="return false" onselectstart="return false"
加在body中




2.1  表單項不能爲空

<script  language="javascript">
<!--
function  CheckForm()
{
if  (document.form.name.value.length  ==  0)  {
alert("請輸入您姓名!");
document.form.name.focus();
return  false;
}
return  true;
}
-->
</script>

2.2  比較兩個表單項的值是否相同

<script  language="javascript">
<!--
function  CheckForm()
if  (document.form.PWD.value  !=  document.form.PWD_Again.value)  {
alert("您兩次輸入的密碼不一樣!請重新輸入.");
document.ADDUser.PWD.focus();
return  false;
}
return  true;
}
-->
</script>

2.3  表單項只能爲數字和"_",用於電話/銀行帳號驗證上,可擴展到域名註冊等

<script  language="javascript">
<!--
function  isNumber(String)
{
var  Letters  =  "1234567890-";  //可以自己增加可輸入值
var  i;
var  c;
if(String.charAt(  0  )=='-')
return  false;
if(  String.charAt(  String.length  -  1  )  ==  '-'  )
return  false;
for(  i  =  0;  i  <  String.length;  i  ++  )
{
c  =  String.charAt(  i  );
if  (Letters.indexOf(  c  )  <  0)
return  false;
}
return  true;
}
function  CheckForm()
{
if(!  isNumber(document.form.TEL.value))  {
alert("您的電話號碼不合法!");
document.form.TEL.focus();
return  false;
}
return  true;
}
-->
</script>


2.4  表單項輸入數值/長度限定

<script  language="javascript">
<!--
function  CheckForm()
{
if  (document.form.count.value  >  100  ||  document.form.count.value  <  1)
{
alert("輸入數值不能小於零大於100!");
document.form.count.focus();
return  false;
}
if  (document.form.MESSAGE.value.length<10)
{
alert("輸入文字小於10!");
document.form.MESSAGE.focus();
return  false;
}
return  true;
}
//-->
</script>

2.5  中文/英文/數字/郵件地址合法性判斷

<SCRIPT  LANGUAGE="javascript">
<!--

function  isEnglish(name)  //英文值檢測
{
if(name.length  ==  0)
return  false;
for(i  =  0;  i  <  name.length;  i++)  {
if(name.charCodeAt(i)  >  128)
return  false;
}
return  true;
}

function  isChinese(name)  //中文值檢測
{
if(name.length  ==  0)
return  false;
for(i  =  0;  i  <  name.length;  i++)  {
if(name.charCodeAt(i)  >  128)
return  true;
}
return  false;
}

function  isMail(name)  //  E-mail值檢測
{
if(!  isEnglish(name))
return  false;
i  =  name.indexOf("  at  ");
j  =  name  dot  lastIndexOf("  at  ");
if(i  ==  -1)
return  false;
if(i  !=  j)
return  false;
if(i  ==  name  dot  length)
return  false;
return  true;
}

function  isNumber(name)  //數值檢測
{
if(name.length  ==  0)
return  false;
for(i  =  0;  i  <  name.length;  i++)  {
if(name.charAt(i)  <  "0"  ||  name.charAt(i)  >  "9")
return  false;
}
return  true;
}

function  CheckForm()
{
if(!  isMail(form.Email.value))  {
alert("您的電子郵件不合法!");
form.Email.focus();
return  false;
}
if(!  isEnglish(form.name.value))  {
alert("英文名不合法!");
form.name.focus();
return  false;
}
if(!  isChinese(form.cnname.value))  {
alert("中文名不合法!");
form.cnname.focus();
return  false;
}
if(!  isNumber(form.PublicZipCode.value))  {
alert("郵政編碼不合法!");
form.PublicZipCode.focus();
return  false;
}
return  true;
}
//-->
</SCRIPT>

2.6  限定表單項不能輸入的字符

<script  language="javascript">
<!--

function  contain(str,charset)//  字符串包含測試函數
{
var  i;
for(i=0;i<charset.length;i++)
if(str.indexOf(charset.charAt(i))>=0)
return  true;
return  false;
}

function  CheckForm()
{
if  ((contain(document.form.NAME.value,  "%/(/)><"))  ||  (contain(document.form.MESSAGE.value,  "%/(/)><")))
{
alert("輸入了非法字符");
document.form.NAME.focus();
return  false;
}
return  true;
}
//-->
</script>

1. 檢查一段字符串是否全由數字組成     
---------------------------------------     
<script language="Javascript"><!--         
function checkNum(str){return str.match(//D/)==null}         
alert(checkNum("1232142141"))         
alert(checkNum("123214214a1"))         
// --></script>       
    
2. 怎麼判斷是否是字符       
---------------------------------------     
if (/[^/x00-/xff]/g.test(s)) alert("含有漢字");        
else alert("全是字符");    
         
3. 怎麼判斷是否含有漢字      
---------------------------------------      
if (escape(str).indexOf("%u")!=-1) alert("含有漢字");       
else alert("全是字符");         
  
4. 郵箱格式驗證  
---------------------------------------    
//函數名:chkemail   
//功能介紹:檢查是否爲Email Address   
//參數說明:要檢查的字符串   
//返回值:0:不是 1:是   
function chkemail(a)   
{ var i=a.length;   
var temp = a.indexOf('@');   
var tempd = a.indexOf('.');   
if (temp > 1) {   
if ((i-temp) > 3){   
if ((i-tempd)>0){   
return 1;   
}   
  
}   
}   
return 0;   
}   
  
5. 數字格式驗證  
---------------------------------------    
//函數名:fucCheckNUM   
//功能介紹:檢查是否爲數字   
//參數說明:要檢查的數字   
//返回值:1爲是數字,0爲不是數字   
function fucCheckNUM(NUM)   
{   
var i,j,strTemp;   
strTemp="0123456789";   
if ( NUM.length== 0)   
return 0   
for (i=0;i<NUM.length;i++)   
{   
j=strTemp.indexOf(NUM.charAt(i));   
if (j==-1)   
{   
//說明有字符不是數字   
return 0;   
}   
}   
//說明是數字   
return 1;   
}   
  
6. 電話號碼格式驗證  
---------------------------------------    
//函數名:fucCheckTEL   
//功能介紹:檢查是否爲電話號碼   
//參數說明:要檢查的字符串   
//返回值:1爲是合法,0爲不合法   
function fucCheckTEL(TEL)   
{   
var i,j,strTemp;   
strTemp="0123456789-()# ";   
for (i=0;i<TEL.length;i++)   
{   
j=strTemp.indexOf(TEL.charAt(i));   
if (j==-1)   
{   
//說明有字符不合法   
return 0;   
}   
}   
//說明合法   
return 1;   
}  
 
7. 判斷輸入是否爲中文的函數 
---------------------------------------    
function ischinese(s){ 
var ret=true; 
for(var i=0;i<s.length;i++) 
ret=ret && (s.charCodeAt(i)>=10000); 
return ret; 
}  
 
8. 綜合的判斷用戶輸入的合法性的函數
---------------------------------------    
<script language="javascript">
//限制輸入字符的位數開始
//m是用戶輸入,n是要限制的位數
function issmall(m,n)
{
if ((m<n) && (m>0))
  {
  return(false);
  }
else
{return(true);}
}

9. 判斷密碼是否輸入一致
---------------------------------------    
function issame(str1,str2)
{
if (str1==str2)
{return(true);}
else
{return(false);}
}

10. 判斷用戶名是否爲數字字母下滑線
---------------------------------------    
function notchinese(str){
var reg=/[^A-Za-z0-9_]/g
    if (reg.test(str)){
    return (false);
    }else{
return(true);    }
}

11. form文本域的通用校驗函數
---------------------------------------    
作用:檢測所有必須非空的input文本,比如姓名,賬號,郵件地址等等。
該校驗現在只針對文本域,如果要針對form裏面的其他域對象,可以改變判斷條件。

使用方法:在要檢測的文本域中加入title文字。文字是在提示信息,你要提示給用戶的該字段的中文名。比如要檢測用戶名
html如下<input name="txt_1" title="姓名">,當然,最好用可視化工具比如dreamweaver什麼的來編輯域。
如果要檢測數字類型數據的話,再把域的id統一爲sz.
javascript判斷日期類型比較麻煩,所以就沒有做日期類型校驗的程序了.高手可以補充。

程序比較草,只是提供一個思路。拋磚引玉! :)
哦,對了,函數調用方法:< form  οnsubmit="return dovalidate()">

function dovalidate()
{
fm=document.forms[0] //只檢測一個form,如果是多個可以改變判斷條件
    for(i=0;i<fm.length;i++)
    {
    //檢測判斷條件,根據類型不同可以修改
    if(fm[i].tagName.toUpperCase()=="INPUT" &&fm[i].type.toUpperCase()=="TEXT" && (fm[i].title!=""))
  
         if(fm[i].value="/blog/="")//
         {
         str_warn1=fm[i].title+"不能爲空!";
         alert(str_warn1);
        fm[i].focus();
         return false;       
         }
         if(fm[i].id.toUpperCase()=="SZ")//數字校驗
         {
                 if(isNaN(fm[i].value))
                { str_warn2=fm[i].title+"格式不對";
                alert(str_warn2);
                fm[i].focus();
                 return false;
                 }
        }
    }
    return true;
}


2 >表單提交驗證類


2.1 表單項不能爲空

<script language="javascript">
<!--
function CheckForm()
{
if (document.form.name.value.length == 0) {
alert("請輸入您姓名!");
document.form.name.focus();
return false;
}
return true;
}
-->
</script>

2.2 比較兩個表單項的值是否相同

<script language="javascript">
<!--
function CheckForm()
if (document.form.PWD.value != document.form.PWD_Again.value) {
alert("您兩次輸入的密碼不一樣!請重新輸入.");
document.ADDUser.PWD.focus();
return false;
}
return true;
}
-->
</script>

2.3 表單項只能爲數字和"_",用於電話/銀行帳號驗證上,可擴展到域名註冊等

<script language="javascript">
<!--
function isNumber(String)
{
var Letters = "1234567890-"; //可以自己增加可輸入值
var i;
var c;
if(String.charAt( 0 )=='-')
return false;
if( String.charAt( String.length - 1 ) == '-' )
return false;
for( i = 0; i < String.length; i ++ )
{
c = String.charAt( i );
if (Letters.indexOf( c ) < 0)
return false;
}
return true;
}
function CheckForm()
{
if(! isNumber(document.form.TEL.value)) {
alert("您的電話號碼不合法!");
document.form.TEL.focus();
return false;
}
return true;
}
-->
</script>


2.4 表單項輸入數值/長度限定

<script language="javascript">
<!--
function CheckForm()
{
if (document.form.count.value > 100 || document.form.count.value < 1)
{
alert("輸入數值不能小於零大於100!");
document.form.count.focus();
return false;
}
if (document.form.MESSAGE.value.length<10)
{
alert("輸入文字小於10!");
document.form.MESSAGE.focus();
return false;
}
return true;
}
//-->
</script>

2.5 中文/英文/數字/郵件地址合法性判斷

<SCRIPT LANGUAGE="javascript">
<!--

function isEnglish(name) //英文值檢測
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charCodeAt(i) > 128)
return false;
}
return true;
}

function isChinese(name) //中文值檢測
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charCodeAt(i) > 128)
return true;
}
return false;
}

function isMail(name) // E-mail值檢測
{
if(! isEnglish(name))
return false;
i = name.indexOf(" at ");
j = name dot lastIndexOf(" at ");
if(i == -1)
return false;
if(i != j)
return false;
if(i == name dot length)
return false;
return true;
}

function isNumber(name) //數值檢測
{
if(name.length == 0)
return false;
for(i = 0; i < name.length; i++) {
if(name.charAt(i) < "0" || name.charAt(i) > "9")
return false;
}
return true;
}

function CheckForm()
{
if(! isMail(form.Email.value)) {
alert("您的電子郵件不合法!");
form.Email.focus();
return false;
}
if(! isEnglish(form.name.value)) {
alert("英文名不合法!");
form.name.focus();
return false;
}
if(! isChinese(form.cnname.value)) {
alert("中文名不合法!");
form.cnname.focus();
return false;
}
if(! isNumber(form.PublicZipCode.value)) {
alert("郵政編碼不合法!");
form.PublicZipCode.focus();
return false;
}
return true;
}
//-->
</SCRIPT>

2.6 限定表單項不能輸入的字符

<script language="javascript">
<!--

function contain(str,charset)// 字符串包含測試函數
{
var i;
for(i=0;i<charset.length;i++)
if(str.indexOf(charset.charAt(i))>=0)
return true;
return false;
}

function CheckForm()
{
if ((contain(document.form.NAME.value, "%/(/)><")) || (contain(document.form.MESSAGE.value, "%/(/)><")))
{
alert("輸入了非法字符");
document.form.NAME.focus();
return false;
}
return true;

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