自己整理 可用的 表单验证(表单项不能为空)js自定义trim操作

来源:互联网 发布:tomcat war 部署端口 编辑:程序博客网 时间:2024/06/04 19:28

<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
String.prototype.trim = function() { return this.replace(/(^\s+)|(\s+$)/g, ''); }
function check(obj){
if(obj.value.length <=0 ||obj.value.trim()=="") {

document.getElementById("errorinfo").style.display = "block";
document.getElementById("submit").disabled = true;
} else {
 
  document.getElementById("errorinfo").style.display = "none";
  document.getElementById("submit").disabled = false; 
  }    
}
</script>
</head>
<body>
<!--
<p style="color:#FF0000" align="center">温馨提示:带&quot;*&quot;为必填内容</p>
-->
<form name="form1" method="post" action="www.baidu.com" >
  <table>
    <tr>
      <td >账号:</td>
      <td ><input name="username" type="text" id="username" onblur="check(this)"/>
   * <div style="display:none" id="errorinfo"><font color="red">账号不能为空</font></div></td>
    
    </tr>
    <tr>
   
      <td >
       <input type="submit" value="提交"  id="submit" style="width:50px" disabled="disabled"/>    &nbsp;&nbsp;
      </td>
    </tr>
  </table>
</form>
</body>

原创粉丝点击