1.动态复制文本框内容
document.myForm.copyText.value = document.myForm.myText.value
-------------------
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.只能是英文
<script language=javascript>
function onlyEng()
{
if(!(event.keyCode>=65&&event.keyCode<=90))
event.returnvalue=false;
}
</script>
<input οnkeydοwn="onlyEng();">
3. 只能是数字
<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();">
4.两次输入密码是否相同
<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){ //document.all兼容性不好,慎用
if(input1.value!=input2.value)
{
alert("false")
input1.value = "";
input2.value = "";
}
else document.forms[0].submit();
}
}
</script>
5.屏蔽右键
οncοntextmenu="return false" οndragstart="return false" onselectstart="return false"
加在body中
6.表单不能为空
function CheckForm()
{
if (document.form.name.value.length == 0) {
alert("请输入您姓名!");
document.form.name.focus();
return false;
}
return true;
}
7.反选
<form id="form1" name="form1" method="post" action="">
<p align="center">
<input type="checkbox" name="checkbox" value="checkbox" id="basketball"/>
篮球
<input type="checkbox" name="checkbox" value="checkbox" id="football" />
足球
<input type="checkbox" name="checkbox" value="checkbox" id="pingpong" />
兵乓球
<input name="button" type="button" id="button" value="反选 "οnclick="setcheckbox()" />
</p>
</form>
javascript代码:
function setcheckbox()
{
var boxs=document.getElementsByName("checkbox");
for(var i=0;i<boxs.length;i++)
{
boxs[i].checked=!boxs[i].checked;
}
}
8.判断多选框有没有被选中
function checkselect()
{
var cs=document.getElementsByName("checkbox2");
var flag=false;
for(var c=0;c<cs.length;c++)
{
if(cs[c].checked)
{
flag=true;
break;
}
}
if(flag)
{
alert("复选框有选中");
}
else
{
alert("请至少选择一个复选框");
}
}