js验证表单大全
1. 长度限制-[ }C/5C&VP+vS}oA
<script>%mE8C u(}2u N}e/_
function test() -mr-}6DSil
{"U+|A.W Nmmm,V
if(document.a.b.value.length>50)
9YO Tp(EbW { {'/duEF^ Rb0w
alert("不能超过50个字符!");/o;p*aFI,NI!@&r^A
document.a.b.focus();0^?"[nj2]
return false;+YBG-}BP3Q.[+a
}
r:w%p{o8? }
(ES/[f D? yX </script>$qD o!L9au6B}
<form name=a οnsubmit="return test()">
k#jA+Q&@'?w9LlR$f <textarea name="b" cols="40" wrap="VIRTUAL" rows="6"></textarea>
4}aa7[|.U~9AJT0F <input type="submit" name="Submit" value="check">
'g0h"r-gl _t QA"e </form> +I }g$lec
B5dK3YB B 2. 只能是汉字
ybvc{Q n j"w'x <input οnkeyup="value="/oblog/value.replace(/[^/u4E00-/u9FA5]/g,'')">Azn r M)^1hK7q
&vFE%V;X3h
3." 只能是英文%[?/@ / |@O x.w VpI
<script language=javascript>E{)n[9`b
function onlyEng()
e;G9F*|b/P {
$o.qf/1^U/q!D if(!(event.keyCode>=65&&event.keyCode<=90))
D2`9G9lDq event.returnvalue=false;F ~zYk)W3bzI
}G&Da-I:zj
</script>
xh7d6Jtis
Pfk2H)hG <input οnkeydοwn="onlyEng();">w4M0F)I)@Xz/?]
0S5r AGAK`
4. 只能是数字 DP?Kg y ^[
<script language=javascript>R6Hky8X6F
function onlyNum()
V n]~H {
|&g olXj9B if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))) u#~n [7eC'/I
//考虑小键盘上的数字键
fd(^)[U,wy7q#dUU event.returnvalue=false;*T[ u'r0g1C
}B:m Z2r3j
</script>
P {E7A nJ
Z l/bl/P <input οnkeydοwn="onlyNum();">$w/r$} K'K!N8o B&JB[
E+KuP/MW5K 5. 只能是英文字符和数字
|IC5T)d_O'`;lX <input οnkeyup="value="/oblog/value.replace(/[/W]/g,"'')
B[ ZGh#[/g
O'?/U.RD$S(} "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/tl9q&`^x
G@2^2X u
[^/d]/g,''))">
8j}o,^J bU6pQNp4X,Tu
6. 验证油箱格式,B;[,S;Ow
<SCRIPT LANGUAGE=javascript RUNAT=Server>
3_t1o+N!sa3Hc!^ function isEmail(strEmail) {
&Rr W,T/6uC if (strEmail.search(/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]Mk^y:b
Ff:uQK:fV,] +$/) != -1)Og`/S)gt2}
return true;
&y/^In ts5Z elsef)johUI#P2}]N
alert("oh");jg!C]q1y
}*s,[1@)mcq#Jlrl
</SCRIPT>:?;r1J(Q}
<input type=text οnblur=isEmail(this.value)>
^0t8|]Q
/p6q C+bo3^O6ug rB 7. 屏蔽关键字(这里屏蔽***和****)
;~u_h:GbO:xA <script language="javascript1.2">
||9K*S#k function test() {n8tTf{+oLRP
if((a.b.value.indexOf ("***") == 0)||(a.b.value.indexOf ("****") == 0)){~F4q*U&Ng A
alert(":)");
(w:@wW.fmGO%/ a.b.focus();Lt#].YT Q{
return false;}
*xJEJ'nO| }V7w OdW
</script>HuJuK8~(e8U0C?
<form name=a οnsubmit="return test()">{ N4}{u V
<input type=text name=b>
XZe4R!TD#n <input type="submit" name="Submit" value="check">;O5{}L ?E@~ H!l;m
</form>
]3b#vy/o8`V6K ^Pi l6r b
8. 两次输入密码是否相同
N,Bt9Jb,c!K <FORM METHOD=POST ACTION="">k+YJ+J Cj
<input type="password" id="input1">x JvOq$Y
<input type="password" id="input2">;f}0ow#{$ZF
<input type="button" value="test" οnclick="check()">T7YRnak h
</FORM>
5eEmNz <script>wM2`4g`
function check()
;DEy)g)uUT { c n^L#]]D
with(document.all){
/MNyoU~E if(input1.value!=input2.value)
F?.so"HN {An l G[I*n
alert("false")~*U.GL3iSs p
input1.value = "";
3i"M%l5w7Ly(U/4[ input2.value = "";0VJqd:C4CP
}/j(Y9uF,K(Yk']6J{&G
else document.forms[0].submit();
;oLko ~;Rs"D3k }"d2T7z,K.oEiZe)Q
}
F%@.r1R_t%d(Gh$S </script>4c4z6EgQ
够了吧 :)kY5v pm.Y3n
屏蔽右键 很酷
oe eEP$X οncοntextmenu="return false" οndragstart="return false" onselectstart="return false"
&D8Z%N1S#tH,e 加在body中t2x#[!s4Wk }t
2.1 表单项不能为空
<scrīpt language="javascrīpt">
<!--
function CheckForm()
{
if (document.form.name.value.length == 0) {
alert("请输入您姓名!");
document.form.name.focus();
return false;
}
return true;
}
-->
</scrīpt>
2.2 比较两个表单项的值是否相同
<scrīpt language="javascrīpt">
<!--
function CheckForm()
if (document.form.PWD.value != document.form.PWD_Again.value) {
alert("您两次输入的密码不一样!请重新输入.");
document.ADDUser.PWD.focus();
return false;
}
return true;
}
-->
</scrīpt>
2.3 表单项只能为数字和"_",用于电话/银行帐号验证上,可扩展到域名注册等
<scrīpt language="javascrīpt">
<!--
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;
}
-->
</scrīpt>
2.4 表单项输入数值/长度限定
<scrīpt language="javascrīpt">
<!--
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;
}
//-->
</scrīpt>
2.5 中文/英文/数字/邮件地址合法性判断
<scrīpt LANGUAGE="javascrīpt">
<!--
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;
}
//-->
</scrīpt>
文章出处:http://www.diybl.com/course/1_web/javascript/jsjs/2008224/101449_2.html