这是我用于验证空字段的javascript
$(document).ready(function() {
$('#submit').click(function() {
var crop = $('#crop').val();
//Empty crop
if (crop === "") {
$('#cropEr2').show();
return false;
}
else {
$('#cropEr2').hide();
}
});
});
表格在下面
<form name="myForm" action="../controller/new.php" method="POST" enctype="multipart/form-data">
<table align="left" width="300" height="200">
<tbody>
<tr><td>Crop Name : </td>
<td><input type="text" name="crop_id" value=""size="45" id="crop" onkeyup="checkLetters(this.value);"/>
<div id="cropEr2" style="display:none;color:red">Enter crop name</div>
<div id="cropEr3" style="display:none;color:red">You have to enter only letters.</div> </td></tr>
<tr><td> <input class="button" type="reset" value="Clear Deatils" /></td>
<td><input type="hidden" name="action" value="add_crop" />
<input class="button" id="submit" name="submit" type="submit" value="Add Details"/></td> </tr>
</tbody>
</table>
</form>
我只是想知道这段代码有什么问题。它没有验证!!
答案 0 :(得分:1)
如果用户在字段中输入空格,则表单将提交。尝试修剪它们:
var crop = $('#crop').val().trim();