[英]How do i check that the textboxes and checkbox has been filled and checked before user can submit the form?
Below shown my JS code and part of the HTML that i wish to create the form. 下面显示了我的JS代码和我希望创建表单的HTML部分。 I can't find the reason of why i CAN still submit it when there are fields that are not filled.
我找不到在没有填写字段时仍可以提交的原因。 It should pop up a dialog box according to which fields the user has not fill.
它应根据用户尚未填写的字段弹出一个对话框。
<script>
function validation() {
if( $('#sdate').val() == null || $('#sdate').val() == undefined || $('#sdate').val() == "" ){
alert( 'Please fill in start date field');
}
if( $('#edate').val() == null || $('#edate').val() == undefined || $('#edate').val() == "" ){
alert( 'Please fill in end date field');
}
if( $('#agree').val() == null || $('#agree').val() == undefined || $('#agree').val() == ""){
alert( 'Please indicate that you have satisfied all the requirements');
}
}
</script>
<div class="content-wrapper">
<div class="sub-content">
<div>
<p>Start Date:</p>
<input id="sdate" type="date" name="startdate">
</div>
</div>
<div class="sub-content">
<div>
<p>End Date:</p>
<input id="edate" type="date" name="enddate">
</div>
</div>
</div>
<div class="end-content">
<div class="center-align">
<div class="checklist">
<p>By checking this box I agree that I have satisfied all requirements.</p>
<input id="agree" type="checkbox" name="checkbox" class="tick-att">
</div>
<br>
<div class="align-right">
<input type="submit" class="button" name="submit" value="submit" onclick="validation()" >
</div>
</div>
</div>
check box validation was wrong . 复选框验证错误。 Iis
is(':checked')
.And apply the input validation simple with !input value
its validate null
, empty
, undefined
. Iis
is(':checked')
。并使用!input value
简单地应用输入验证,其validate为null
, empty
和undefined
。 .trim()
remove the unwanted empty spaces . .trim()
删除不需要的空格。
if you have from
tag try with onsubmit="return validation()"
instead of onclick="validation"
submit button. 如果您
from
标记,请尝试使用onsubmit="return validation()"
代替onclick="validation"
提交按钮。 return false
its stop the function execution when the input fails return false
当输入失败时停止函数执行
function validation() { if (!$('#sdate').val().trim()) { alert('Please fill in start date field'); return false // if you have a form tag } if (!$('#edate').val().trim()) { alert('Please fill in end date field'); return false // if you have a form tag } if (!$('#agree').is(':checked')) { alert('Please indicate that you have satisfied all the requirements'); return false // if you have a form tag } else{ console.log('ok') } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form onsubmit="return validation()"> <div class="content-wrapper"> <div class="sub-content"> <div> <p>Start Date:</p> <input id="sdate" type="date" name="startdate"> </div> </div> <div class="sub-content"> <div> <p>End Date:</p> <input id="edate" type="date" name="enddate"> </div> </div> </div> <div class="end-content"> <div class="center-align"> <div class="checklist"> <p>By checking this box I agree that I have satisfied all requirements.</p> <input id="agree" type="checkbox" name="checkbox" class="tick-att"> </div> <br> <div class="align-right"> <input type="submit" class="button" name="submit" value="submit" > </div> </div> </div> </from>
Try this, It will help you, 试试这个,它将为您提供帮助,
$("#btn").click(function() { if( $('#sdate').val() == null || $('#sdate').val() == undefined || $('#sdate').val() == "" ){ alert( 'Please fill in start date field'); return false; } if( $('#edate').val() == null || $('#edate').val() == undefined || $('#edate').val() == "" ){ alert( 'Please fill in end date field'); return false; } if(!document.getElementById('agree').checked) { alert( 'Please indicate that you have satisfied all the requirements'); return false; } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="content-wrapper"> <div class="sub-content"> <div> <p>Start Date:</p> <input id="sdate" type="date" name="startdate"> </div> </div> <div class="sub-content"> <div> <p>End Date:</p> <input id="edate" type="date" name="enddate"> </div> </div> </div> <div class="end-content"> <div class="center-align"> <div class="checklist"> <p>By checking this box I agree that I have satisfied all requirements.</p> <input id="agree" type="checkbox" name="checkbox" class="tick-att"> </div> <br> <div class="align-right"> <input type="submit" id="btn" class="button" name="submit" value="submit" onClientClick="validation()" /> </div> </div> </div> </div>
You need to add return false
after every alert. 您需要在每个警报后添加
return false
。 then It will prevent submit event and also it will show the alert message of appropriate field which is first position in the form. 然后它将阻止提交事件,并且还将显示相应字段的警报消息,该字段是表单中的第一位。 If it is not filled immediately the submit event is prevent rest of the alert messages are not shown even the field is empty.
如果未立即填充,则提交事件将被阻止,即使该字段为空,也不会显示其余警报消息。
you need to catch the form submit event. 您需要赶上表单提交事件。 The
return false
statement in the if conditions will stop the event. if条件中的
return false
语句将停止事件。 If everything goes fine, no return false
statement will be executed and the form will be submitted. 如果一切顺利,将不执行
return false
语句,并且将提交表单。
var validation = function() {
if( $('#sdate').val() == null || $('#sdate').val() == undefined || $('#sdate').val() == "" ){
alert( 'Please fill in start date field');
return false;
}
if( $('#edate').val() == null || $('#edate').val() == undefined || $('#edate').val() == "" ){
alert( 'Please fill in end date field');
return false;
}
if( !$('#agree').is(':checked') ){
alert( 'Please indicate that you have satisfied all the requirements');
return false;
}
}
$("form").submit(function(){
validation();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.