我试图验证表单使用JavaScript,但表单验证不起作用。不确定,如果它甚至被路由到该函数。这里是html表单模式。JavaScript-HTML表单验证不起作用
<div class="modal-body form">
<form action="#" id="form" class="form-horizontal" name="myForm" onsubmit="return validateForm()" ;>
<input type="hidden" value="" name="id" />
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Brand Name</label>
<div class="col-md-9">
<input required type="text" name="bname" id="brname" class="form-control" placeholder="Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Brand Origin</label>
<div class="col-md-9">
<input required type="text" name="origin" id="brorigin" class="form-control" placeholder="Origin">
</div>
</div>
</div>
</form>
<div class="modal-footer">
<input type="submit" id="btnSave" value="Submit" onclick="save()" class="btn btn-primary"></input>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
这里是javascript的表单验证脚本
function validateForm()
{
$('#btnSave').click(function()
{
var brname = document.getElementById("brname").value;
var brorigin = document.getElementById("brorigin").value;
if (brname.length == 0)
{
window.alert("You must enter a name.");
brname.focus();
return false;
}
if (brorigin.length == 0)
{
window.alert("You must enter the origin country");
brorigin.focus();
return false;
}
});
}
感谢..
编辑:有形式模型prolem。 onclick事件监听器将保存表单。当我在$(document).ready(function(){
函数中移动函数时显示错误警告
你有没有试图改变 'x.length == 0' 到 'x.length === 0'? – MorganR
您是否尝试过移动表单标签内的提交按钮? –
请指定“表单验证无效”。什么**正确**错误?代码的预期结果是什么? – gus27