[英]JS Form validation is not working For html form
我有一个简单的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;
}
}
一切看起来都很好。 但是验证不起作用。 相同的代码与另一个相似的代码正在工作。 那我的代码有什么问题呢?
只需在您的validate函数的末尾添加return true
,以便在没有验证错误的情况下提交表单
您的验证对我有用。 首先,尽管我必须填写下拉列表,父母的手机号码和地址,因为html验证首先在javascript验证之前运行。
我必须首先填写的这些字段(在弹出输入您的全名警报之前)都具有必填属性。
检查工作代码,如下所示:
<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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.