我有一个js文件
var regexEnum =
{
    decmal6: "^([1-9]+[0-9]{0,}|[0])(\\.\\d{1,2})?$", //正浮点数,两位小数    code: "^[A-Za-z0-9]*$", //验证码,数字、26个英文字母
    email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
    color: "^[a-fA-F0-9]{6}$",  //颜色
    url: "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //url
    notempty: "^\\S+$",  //非空
    picture: "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
    rar: "(.*)\\.(rar|zip|7zip|tgz)$",  //压缩文件
 }
比如我想写个js验证非空
<input type="text" id="wordName" name="wordName" /><label style=" color:Red">*</label>
让如果input的值是空的话,label的值变为“不能为空”的提示。在js里应该怎么写啊。!求救。最好是个完整的方法啊。

解决方案 »

  1.   

    <input type="text" id="wordName" name="wordName" value='' onblur="document.getElementById('noName').innerHTML=(value=='')?'不能为空':'';"  /><label id="noName" style=" color:Red">*</label>
      

  2.   

    可能不明白你的意思,你看下面的方法行不行<input type="text" id="wordName" name="wordName" onblur="aa(this.value)"/>
    <label id="xx" style=" color:Red">*</label><script type="text/javascript">
    function aa(s)
    {if(s=="")document.getElementById("xx").innerHTML="不能为空!"}
    </script>
      

  3.   


    <html>
    <head>
    <title>Demo</title>
    <script type="text/javascript">
    function CheckName(){
    var obj=document.getElementById("txtName");
    var lObj=document.getElementById("lbError");
    var value=obj.value;
    var msg;
    if(value=="" || value==null)
    msg="不能为空";
    else
    msg="*";
    lObj.innerText=msg;
    }
    </script>
    </head>
    <body>
    <input type="text" id="txtName" onblur="CheckName();" />
    <label id="lbError" style="color:Red;">*</label>
    </body>
    </html>
      

  4.   


    <script>
    var regexEnum =
    {
      decmal6: "^([1-9]+[0-9]{0,}|[0])(\\.\\d{1,2})?$", //正浮点数,两位小数
      code: "^[A-Za-z0-9]*$", //验证码,数字、26个英文字母
      email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
      color: "^[a-fA-F0-9]{6}$", //颜色
      url: "^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //url
      notempty: "^\\S+$", //非空
      picture: "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
      rar: "(.*)\\.(rar|zip|7zip|tgz)$", //压缩文件
     }function validcheck(id){
      var s=document.getElementById(id).innerHTML;
      var r=eval('/'+regexEnum.notempty+'/gi');  if(!r.test(s)){    document.getElementById('lbl').innerHTML="不能为空!"  }
    }
      
    </script>
    <body>
    <input type="text" id="wordName" name="wordName" /><label style=" color:Red" id='lbl'>*</label>
    <br>
    <input type="button" onclick="validcheck('wordName')" value="验证"/>
    </body>
      

  5.   

    看下 jquery.validate 这个jquery插件 有你想要的