0

我想检查以下代码是否将表单字段留空:

<script>

   function validateForm()
   {
       var x=document.forms["myForm"]["name"].value;
       if (x==null || x=="")
         {
            alert(" Name Required* ");
            return false;
         }
    }

</script>

<form action="register2.php" method="POST" id="myForm" onsubmit="return validateForm()" >
<input type="text" name="name"/>
</form>

但它只检查输入框,我如何检查选择框、单选按钮和复选框?

4

3 回答 3

0

像这样使用。

    <script>
          function validateForm(frm)
          {

             if (frm.name.value == "")
             {
                 alert(" Name Required* ");
                 return false;
             }
            if(frm.country.value == 'select')
            {
                 alert(" Please select country ");
                 return false;
            }
         }
    </script>

             <form action="register2.php" method="POST" id="myForm" onsubmit="return validateForm(this)" >
               <input type="text" name="name"/>
             <select name="country">
                  <option value="select">(Please select a country)</option>
                  <option value="pk">Pakistan</option>
                  <option value="chn">China</option>
             </select>
             </form>
于 2013-10-26T10:14:22.900 回答
0
Dude, Use Validate Plugin for Validation

Example:
    $(".selector").validate({
  rules: {
 // simple rule, converted to {required:true}
 name: "required",
 // compound rule
 email: {
  required: true,
email: true
 }
   }
},
messages: {
 name: "Please specify your name",
  email: {
required: "We need your email address to contact you",
email: "Your email address must be in the format of name@domain.com"
}

} }); 验证插件文档

于 2013-10-26T10:24:29.203 回答
0

使用jQuery 表单验证它有你需要验证的所有东西

使用这个简单的例子 http://www.jquery4u.com/forms/basic-jquery-form-validation-tutorial/

于 2013-10-26T10:12:44.337 回答