简体   繁体   English

在用户提交表单之前,如何检查文本框和复选框是否已填写并选中?

[英]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为nullemptyundefined .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.

相关问题 将鼠标悬停在提交/锚点上时如何检查复选框是否已选中并显示警报提示用户这样做 - How to check if checkbox has been checked when hovering over an submit / anchor and display an alert prompting users to do so 如何检查表单中所有必填字段的填写时间? - How can I check when all of the required fields of a form has been filled out? 提交前检查是否已选中复选框 - Check if checkbox is checked before submit 选中复选框后,如何禁用它? - How can I disable a checkbox once it has been checked? 我如何使用jQuery禁用表单的“提交”按钮,直到每个必填字段都已填写? - How do I use jQuery to disable a form's submit button until every required field has been filled? 提交表单检查,如果复选框已选中 - submit form check if checkbox is checked 检查是否已选中google recaptcha或已将其填满 - check if google recaptcha is checked or has been filled 在提交表单之前,如何检查HTML中的表单元素? - How do I check form elements in HTML before the form has been submitted? 在提交表格之前,如何检查文件是否已选择上传 - How can I check if file has been chosen to upload before submitting form 在允许用户提交表单 JS 之前,我如何检查密码是否与确认密码匹配 - How do I check to see is password matches confirm password before user is allowed to submit the form JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM