js判断input是否为空

来源:互联网 发布:阿里云网站模板上传 编辑:程序博客网 时间:2024/05/16 07:47
<SCRIPT LANGUAGE="JavaScript">
function jj(){
if(document.getElementByIdx_x_x('name').value==''){
   alert('用户名不能为空');
   return false;
}
if(document.getElementByIdx_x_x('phone').value==''){
   alert('电话不能为空');
   return false;
}
if(document.getElementByIdx_x_x('password').value==''){
   alert('密码不能为空');
   return false;
}
if(document.getElementByIdx_x_x('password').value != document.getElementByIdx_x_x('password2').value){
   alert('两次输入的密码不一样');
   return false;
}
if(!document.getElementByIdx_x_x('agreement').checked ==true){
   alert('你还没有接受用户协议');
   return false;
}
}
function hh(x,y){
if(document.getElementByIdx_x_x(x).value ==''){
   document.getElementByIdx_x_x(y).innerHTML='带*号的为必填,请不要留下空格';
}
}
</SCRIPT>
<form action="" method="post" onsubmit="return jj()">
<table width="100%" border="0">
    <tr>
        <td width="40%" align="right">用户名:</td>
        <td width="60%"><input type="text" size="25" id="name" name="user_name" onblur="hh('name','name_notice')" />
        <span id="name_notice" style="color:#FF0000">*</span></td>    
    </tr>
    <tr>
        <td width="40%" align="right">电话:</td>
        <td width="60%"><input type="text" size="25" id="phone" name="user_phone" onblur="hh('phone','phone_notice')" />
        <span id="name_notice" style="color:#FF0000">*</span></td>    
    </tr>
    <tr>
        <td width="40%" align="right">密码:</td>
        <td width="60%"><input type="password" size="25" id="password" name="pwd" onblur="hh('password','password_notice')" />
        <span id="password_notice" style="color:#FF0000">*</span></td>
    </tr>
    <tr>
        <td width="40%" align="right">重复密码:</td>
        <td width="60%"><input type="password" size="25" id="password2" name="pwd2" onblur="hh('password2','password_notice')" />
        <span id="password_notice2" style="color:#FF0000">*</span></td>
    </tr>
    <tr>
      <td align="center" colspan="2"><label>
        <input name="agreement" type="checkbox" id='agreement' onblur="hh('agreement','agreement_notice')"/>
        我已看过并接受《<a href="article.php?cat_id=-1" style="color:blue" target="_blank">用户协议</a>》</label></td>
    </tr>
    <tr>
        <td align="center" colspan="2"><input type="submit" value="注册" name="a" /></td>
    </tr>
</table>
0 0
原创粉丝点击