cnblogs/dcrenl/js验证表单大全1.html

148 lines
4.3 KiB
HTML
Raw Permalink Normal View History

2024-09-24 12:43:01 +08:00
<div id="sina_keyword_ad_area2" class="articalContent ">
<div>
<div>附加js验证radio是否选择</DIV>
<div><br /></DIV>
<div>&lt;script
language="javascript"&gt;</DIV>
<div>function checkform(obj)</DIV>
<div>{</DIV>
<div>for(i=0;i&lt;obj.oo.length;i++)</DIV>
<div>&nbsp;<wbr>&nbsp;<wbr> &nbsp;<wbr>
&nbsp;<wbr> &nbsp;<wbr> if(obj.oo[i].checked==true)
return true;</DIV>
<div><br /></DIV>
<div>alert("请选择")</DIV>
<div>return false; &nbsp;<wbr> &nbsp;<wbr>
&nbsp;<wbr> &nbsp;<wbr></DIV>
<div><br /></DIV>
<div>}</DIV>
<div>&lt;/script&gt;</DIV>
<div>&lt;form id="form1" name="form1" method="post"
action="" &nbsp;<wbr> onsubmit="return
checkform(this)"&gt;</DIV>
<div>&nbsp;<wbr>&nbsp;<wbr> &lt;input
type="radio" name="oo" value="radiobutton" /&gt;</DIV>
<div>&nbsp;<wbr>&nbsp;<wbr> &lt;input
type="radio" name="oo" value="radiobutton" /&gt;</DIV>
<div>&nbsp;<wbr>&nbsp;<wbr> &lt;input
type="submit" name="Submit" value="提交" /&gt;</DIV>
<div>&lt;/form&gt;</DIV>
<div><br /></DIV>
<div>1. 长度限制</DIV>
<div>&lt;script&gt;</DIV>
<div>function test()&nbsp;<wbr></DIV>
<div>{</DIV>
<div>if(document.a.b.value.length&gt;50)</DIV>
<div>{</DIV>
<div>alert("不能超过50个字符");</DIV>
<div>document.a.b.focus();</DIV>
<div>return false;</DIV>
<div>}</DIV>
<div>}</DIV>
<div>&lt;/script&gt;</DIV>
<div>&lt;form name=a onsubmit="return
test()"&gt;</DIV>
<div>&lt;textarea name="b" cols="40" wrap="VIRTUAL"
rows="6"&gt;&lt;/textarea&gt;</DIV>
<div>&lt;input type="submit" name="Submit"
value="check"&gt;</DIV>
<div>&lt;/form&gt;&nbsp;<wbr></DIV>
<div><br /></DIV>
<div>2. 只能是汉字&nbsp;<wbr></DIV>
<div>&lt;input
onkeyup="value="/oblog/value.replace(/[^\u4E00-\u9FA5]/g,'')"&gt;</DIV>
<div><br /></DIV>
<div>3." 只能是英文</DIV>
<div>&lt;script
language=javascript&gt;</DIV>
<div>function onlyEng()</DIV>
<div>{</DIV>
<div>
if(!(event.keyCode&gt;=65&amp;&amp;event.keyCode&lt;=90))</DIV>
<div>event.returnvalue=false;</DIV>
<div>}</DIV>
<div>&lt;/script&gt;</DIV>
<div><br /></DIV>
<div>&lt;input
onkeydown="onlyEng();"&gt;</DIV>
<div><br /></DIV>
<div>4. 只能是数字</DIV>
<div>&lt;script
language=javascript&gt;</DIV>
<div>function onlyNum()</DIV>
<div>{</DIV>
<div>
if(!((event.keyCode&gt;=48&amp;&amp;event.keyCode&lt;=57)||(event.keyCode&gt;=96&amp;&amp;event.keyCode&lt;=105)))</DIV>
<div>//考虑小键盘上的数字键</DIV>
<div>event.returnvalue=false;</DIV>
<div>}</DIV>
<div>&lt;/script&gt;</DIV>
<div><br /></DIV>
<div>&lt;input
onkeydown="onlyNum();"&gt;</DIV>
<div><br /></DIV>
<div>5. 只能是英文字符和数字</DIV>
<div>&lt;input
onkeyup="value="/oblog/value.replace(/[\W]/g,"'')
"onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"&gt;</DIV>
<div><br /></DIV>
<div>6. 验证油箱格式</DIV>
<div>&lt;SCRIPT LANGUAGE=javascript
RUNAT=Server&gt;</DIV>
<div>function isEmail(strEmail) {</DIV>
<div>if
(strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)
!= -1)</DIV>
<div>return true;</DIV>
<div>else</DIV>
<div>alert("oh");</DIV>
<div>}</DIV>
<div>&lt;/SCRIPT&gt;</DIV>
<div>&lt;input type=text
onblur=isEmail(this.value)&gt;</DIV>
<div><br /></DIV>
<div>7. 屏蔽关键字(这里屏蔽***和****)</DIV>
<div>&lt;script
language="javascript1.2"&gt;</DIV>
<div>function test() {</DIV>
<div>if((a.b.value.indexOf ("***") == 0)||(a.b.value.indexOf
("****") == 0)){</DIV>
<div>alert("");</DIV>
<div>a.b.focus();</DIV>
<div>return false;}</DIV>
<div>}</DIV>
<div>&lt;/script&gt;</DIV>
<div>&lt;form name=a onsubmit="return
test()"&gt;</DIV>
<div>&lt;input type=text name=b&gt;</DIV>
<div>&lt;input type="submit" name="Submit"
value="check"&gt;</DIV>
<div>&lt;/form&gt;</DIV>
<div><br /></DIV>
<div>8. 两次输入密码是否相同</DIV>
<div>&lt;FORM METHOD=POST
ACTION=""&gt;</DIV>
<div>&lt;input type="password"
id="input1"&gt;</DIV>
<div>&lt;input type="password"
id="input2"&gt;</DIV>
<div>&lt;input type="button" value="test"
onclick="check()"&gt;</DIV>
<div>&lt;/FORM&gt;</DIV>
<div>&lt;script&gt;</DIV>
<div>function check()</DIV>
<div>{&nbsp;<wbr></DIV>
<div>with(document.all){</DIV>
<div>if(input1.value!=input2.value)</DIV>
<div>{</DIV>
<div>alert("false")</DIV>
<div>input1.value = "";</DIV>
<div>input2.value = "";</DIV>
<div>}</DIV>
<div>else document.forms[0].submit();</DIV>
<div>}</DIV>
<div>}</DIV>
<div>&lt;/script&gt;</DIV>
</DIV>
</div>