JS表单验证不适用于html表单

时间:2014-03-24 09:57:29

标签: javascript php html

我有一个简单的HTML表单。表格如下 -

<table>
  <form method="post" action="insert.php"  name="idea_1" id="idea_1" onsubmit="return idea_1_check();" >    
    <input type="hidden" name="type" value="idea" />    
    <tr><td></td><td><br><h3 style="color:#990066; font-weight:bold;">First Member (Team Leader)</h3><br></td></tr> 
    <tr><td></td><td><input type="text" name="stu_name_1" value="" placeholder="Your Full Name"></td></tr>
    <input type="text" name="school_name_1" value="" placeholder="Your School Name"></td></tr>
    <tr><td></td><td><input type="text" name="stu_id_1" value="" placeholder="Your Student ID/Roll"></td></tr> 
    <tr><td></td><td>
      <select name="class_1" id="class" required >
        <option selected="selected" value="">Please Select Your Class</option>
        <option value="Six">Six</option>
        <option value="Seven">Seven</option>
        <option value="Eight">Eight</option>
        <option value="Nine">Nine</option>
        <option value="Ten">Ten</option>
      </select> 
    </td></tr>
    </td><td>
    <select name="division_1" id="division" required>
      <option selected="selected" value="">Please Select Your Division</option>
      <option value="Dhaka">Dhaka</option>
      <option value="Chittagong">Chittagong</option>
      <option value="Sylhet">Sylhet</option>
    </select> 
    </td></tr>
    <tr><td></td><td><input type="text" name="mobile_1" value="" placeholder="Your Parent's Mobile" required id="phone" ></td></tr>
    <tr><td></td><td><input type="text" name="interest_1" value="" placeholder="Your Interest e.g.: Robotics, Physics, Chemistry, Biology "></td></tr>
    <tr><td></td><td><textarea name="address_1" required id="must"="required id="must"" placeholder="Your Full Residential Address"></textarea></td></tr>                                   
    <tr><td></td><td><input type="text" name="email_1" value="" placeholder="Your Email Address" id="email_1"  ><img id="tick_1" src="tick.png" width="16" height="16"/>
    <img id="cross_1" src="cross.png" width="16" height="16"/></td></tr>
</table>
<br>
<p class="submit"><input type="submit" name="commit" value="Register" ></p><br>
<label>
  <div align="center"><a href="http://ignite-bd.com/submit-concept-paper/" target="_blank"><strong>Upload your Concept Paper within 3 weeks (21 Days) to qualify for the divisional round</strong> </a>
  </div>
</label>
<br><br>            
</form>

javascript验证功能如下:

function idea_1_check()
{
  var stu_name=document.forms["idea_1"]["stu_name_1"].value;
  var school=document.forms["idea_1"]["school_name_1"].value;
  var id=document.forms["idea_1"]["stu_id_1"].value;
  var mob=document.forms["idea_1"]["mobile_1"].value;
  var add=document.forms["idea_1"]["address_1"].value;
  var email=document.forms["idea_1"]["email_1"].value;

  var atpos=email.indexOf("@");
  var dotpos=email.lastIndexOf(".");   

  if (stu_name==null || stu_name=="" || stu_name.length<6)
  {
    alert("Please provide Your full name");
    return false;
  }

  if (school==null || school=="" || school.length<6)
  {
    alert("Please provide Your full School name");
    return false;
  }

  if (id==null || id=="" || id.length<1)
  {
    alert("Please provide Your Roll/ID");
    return false;
  }

  if (add=null || add=="" || add.length<10)
  {
    alert("Please provide Your full Residential Address");
    return false;
  }

  else if (email==null || email=="")
  {
    alert("Email must be filled out.");
    return false;
  }

  else if (atpos <1 || dotpos <atpos+2 || dotpos + 2 >= email.length)
  {
    alert("Not a valid e-mail address");
    return false;
  }
}

一切都很好看。但验证不起作用。与另一个类似的代码相同的代码正在工作那我的代码有什么问题?

3 个答案:

答案 0 :(得分:3)

只需在验证功能结束时添加return true,以便在没有验证错误的情况下提交表单

答案 1 :(得分:1)

您的验证对我有用。起初虽然我必须填写下拉菜单,但是在javascript验证之前,父母手机号码和地址作为html验证首先运行。

我必须首先填写的这些字段(在输入您的全名警报之前弹出)都具有必需的属性。

答案 2 :(得分:1)

检查工作代码如下:

<table>
    <form method="post" action="insert.php" name="idea_1" id="idea_1"
        onsubmit="return idea_1_check();">

        <input type="hidden" name="type" value="idea" />
        <tr>
            <td></td>
            <td><br>
            <h3 style="color: #990066; font-weight: bold;">First Member
                    (Team Leader)</h3>
                <br></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="text" name="stu_name_1" value=""
                placeholder="Your Full Name" required="required"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="text" name="school_name_1" value=""
                placeholder="Your School Name" required="required"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="text" name="stu_id_1" value=""
                placeholder="Your Student ID/Roll" required="required"></td>
        </tr>
        <tr>
            <td></td>
            <td><select name="class_1" id="class" required="required" >
                    <option selected="selected" value="">Please Select Your
                        Class</option>
                    <option value="Six">Six</option>
                    <option value="Seven">Seven</option>
                    <option value="Eight">Eight</option>
                    <option value="Nine">Nine</option>
                    <option value="Ten">Ten</option>
            </select></td>
        </tr>
        <tr>
            <td></td>
            <td><select name="division_1" id="division" required="required">
                    <option selected="selected" value="">Please Select Your
                        Division</option>
                    <option value="Dhaka">Dhaka</option>

                    <option value="Chittagong">Chittagong</option>

                    <option value="Sylhet">Sylhet</option>

            </select></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="text" name="mobile_1" value=""
                placeholder="Your Parent's Mobile" required="required" id="phone"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="text" name="interest_1" value=""
                placeholder="Your Interest e.g.: Robotics, Physics, Chemistry, Biology " required="required"></td>
        </tr>
        <tr>
            <td></td>
            <td><textarea name="address_1" id="must" required="required"
                     placeholder="Your Full Residential Address"></textarea></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="text" name="email_1" value=""
                placeholder="Your Email Address" id="email_1" required="required"><img
                id="tick_1" src="tick.png" width="16" height="16" /> <img
                id="cross_1" src="cross.png" width="16" height="16" /></td>
        </tr>
</table>
<br>
<p class="submit">
    <input type="submit" name="commit" onclick="return idea_1_check();" value="Register">
</p>
<br>
<label>
    <div align="center">
        <a href="http://ignite-bd.com/submit-concept-paper/" target="_blank"><strong>Upload
                your Concept Paper within 3 weeks (21 Days) to qualify for the
                divisional round</strong> </a>
    </div>
</label>
<br>
<br>
</form>
</table>

以下是工作副本的链接:http://jsfiddle.net/CU9gP/1/