常用JS表單驗證

2021-05-07 13:17:49 字數 2786 閱讀 2909

專案中會經常使用的一些表單驗證資訊,包含空字元及空格驗證,驗證非法字元,是否數字驗證。

//表單驗證檢查空格

function chkspace(str)

else

return (str==strs);}}

//表單驗證檢查非法字元

function chkfilter(str)

return false;

}//表單驗證檢查數字

function chknumber(str)

return false;

}//檢查郵件的合法性,str:待檢查字元,fs_alert:錯誤資訊顯示容器

function checkmail(str,fs_alert)

else/w*)(/./w*)/i;

re.exec(mail);

if (regexp.$3!=」"&®exp.$3!=」.」&®exp.$2!=」.」) strr=regexp.$1+regexp.$2+regexp.$3

else

if (regexp.$2!=」"&®exp.$2!=」.」) strr=regexp.$1+regexp.$2

else strr=regexp.$1

if (strr!=mail)

}if (fs_alert!=」")else

}elseelse}}

catch(e)

}//檢查日期的合法性,字段說明同上

function valiatedate(str,fs_alert)

if (dar==」") return;

if(dar.split(」-」)[0].length==2)

var datepat = /^(/d)(-)(/d)(-)(/d)$/;

var matcharray = dar.match(datepat); // is the format ok?

if (matcharray == null)

month = matcharray[3]; // parse date into variables

day = matcharray[5];

year = matcharray[1];

if (month < 1 || month > 12)

if (day < 1 || day > 31)

if ((month==4 || month==6 || month==9 || month==11) && day==31)

if (month == 2)

if (day==29 && !isleap)

}document.getelementbyid(fs_alert).innerhtml=」"

return true;

}//檢查字元是否為數字,str:待檢查字元;fs_alert:錯誤資訊顯示容器;isinteger:是否為整數

function isnumber(str,fs_alert,msg,isinteger)

else if(isnan(obj.value)||obj.value<0)

else if(!isnan(obj.value)&&obj.value>=0)

else

}else}}

//檢查字元是否為空,str:待檢查字元;fs_alert:錯誤資訊顯示容器

function isempty(str,fs_alert)

else

else if (str_len > 50 && str_len <= 100)

else if (str_len > 100)

document.getelementbyid(fs_alert).innerhtml=」字數:」 + str_len + 「

」;return true;}}

//檢查字元是否為中文,str:待檢查字元;fs_alert:錯誤資訊顯示容器

function ischinese(str,fs_alert)

}document.getelementbyid(fs_alert).innerhtml=」";

return true

}以下是一些常用的js正則:

Js 表單驗證

兩個日期比較 用途 檢查開始日期是否小於等於結束日期 輸入 s 字串 開始日期 格式 2001 5 4 e 字串 結束日期 格式 2002 5 4 返回 如果通過開始日期小於等於結束日期返回 true,否則返回 false function data compare s,e d d if r nul...

JS表單驗證

1.長度限制 2.只能是漢字 3.只能是英文本母 4.只能是數字 5.只能是英文本母和數字 6.檢驗時間大小 與當前時間比較 7.遮蔽關鍵字 這裡遮蔽 和 8.兩次輸入密碼是否相同 9.表單項不能為空 10.郵箱驗證 11.驗證手機號 12.驗證身份證號碼 需是有效身份證 13.js正規表示式 例子...

js表單驗證

列出一些常用的格式驗證的js方法,以備查詢。檢查是否為數字 function checkisinteger str if str.search 0 9 0 else 檢查是否整數 function checknum str str str 1 tostring if str.indexof 1 re...