JS表单验证

 

1.Form 对象

Document

    |

Form

    |

Text Textarea Password Radio Checkbox Button Reset Submit Select Option Hidden

----------------------------------

示例:

<form action="2.html" method="get" name="form1" οnsubmit="return check();">

username:<input name="user" type="text" />

<br><br>

password:<input name="password" type="password" />

<br><br>

<input name="submit" type="submit" value="提交" />

<input name="reset" value="重置" type="reset" />

</form>

<script type="text/javascript">
 function check(){
  /*
  if(document.getElementsByName("user")[0].value=="" || document.getElementsByName("user")[0].value==null)
   alert("username is null ");
  if(document.getElementsByName("password")[0].value=="" || document.getElementsByName("password")[0].value==null)
   alert("password is null ");
  */
  with(document){
   alert(form1.user.value+forms[0].user.value+" "+forms[0].elements[0].value+"/n/r"+
     form1.password.value+"/n/r" );
  }
  return false;

 }
</script>

 

 2.CODE

--------------------------------

<form action="2.html" method="get" name="form1" οnsubmit="return check();">

username:<input name="user" type="text" />

<br><br>

password:<input name="password" type="password" />

<br><br>

email:<input name="email" type="text" size="20" value="amos_tl@126.com" οnclick="checkEmail();" οnselect="checkEmail();" />

<br><br>

sex:
<input name="sex" type="radio" value="男" checked>男</input>
<input name="sex" type="radio" value="女"> 女</input>

<br><br>

你是中国人吗?
<br>
<input name="checkbox" type="checkbox" value="是" checked>是</input>
<input name="checkbox" type="checkbox" value="是的">是的</input>
<input name="checkbox" type="checkbox" value="我是的">我是的</input>
<input name="checkbox" type="checkbox" value="我是中国人" checked>我是中国人</input>

<br><br>

下拉菜单演示<br>
 <select name="downdroplist">
  <option value="http://www.baidu.com">baidu</option>
  <option value="http://www.sina.com">sina</option>
  <option value="http://www.csdn.net">csdn</option>
  <option value="http://6.cn">6.cn</option>
 </select>
 <input name="btn" type="button" value="go" οnclick="goUrl();" />


<input name="submit" type="submit" value="提交" />

<input name="reset" value="重置" type="reset" />

</form>

<script type="text/javascript">
 function goUrl(){
  var url="";
  var i=document.form1.downdroplist.selectedIndex;
  //alert(document.form1.downdroplist.selectedIndex);
  url=document.form1.downdroplist.options[i].value;
  
  window.open(url);
  //location=url;
 }

 function check(){
  /*
  if(document.getElementByName("user").value=="" || document.getElementByName("user").value==null)
   alert("username is null");
  if(document.getElementByName("password").value=="" || document.getElementByName("password").value==null)
   alert("password is null");
  */
  with(document){
   alert(document.getElementsByName("user")[0].value+" "+forms[0].user.value+" "+forms[0].elements[0].value+"/n/r"+
     form1.password.value+"/n/r"+
     form1.sex[0].value+"/n/r"+
     form1.sex[1].value+"/n/r"+
     form1.checkbox[0].value+"/n/r"+
     form1.checkbox[1].value+"/n/r"+
     form1.checkbox[2].value+"/n/r"+form1.checkbox[3].checked+
     form1.checkbox[3].value+"/n/r"
   );
  }
  
  
  return false;

 }

 function checkEmail(){
  var e=document.form1.email.value;
  if(e=="" || e.indexOf('@')==-1 || e.indexOf('.')==-1 || e.indexOf('@')>=e.indexOf('.'))
  {
   alert("E-mail不合法!");
   document.form1.email.value="amos_tl@126.com";
   return false
  }
  
  
  return true;

 }
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值