I have a form in which I want to get a message and a date from the user. None of them can be empty upon the form submission.
I prevent form submission if the text field is empty(which is a CKeditor rich text field) and it works just fine. But, when I want to check the date( no dates before today can be submitted), it's not working!
Here is what I am trying to do:
<form name="submit_quote" id="submit_quote" action="{% url 'submit_quote' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type = 'date'
class="input_date"
placeholder="yyyy-mm-dd"
name='when_is_required_date_{{ item.tag }}{{ item.id }}'
id='when_is_required_date_{{ item.tag }}{{ item.id }}'
required
min="2021-01-31"
pattern="\d{4}-\d{2}-\d{2}">
<textarea name="message"
id="message"
class="form-control"
maxlength="4096"
onkeyup="textCounter(this,'counter',4096);"
autofocus required
style="height: 150px !important; " ></textarea>
<input
disabled
maxlength="4"
size="4"
value="4096"
id="counter"
style="background-color: rgb(255, 255, 255); border: none; right: 0px;" >
<div class="form-row">
<div class="col-12 col-sm-12 col-md-2 col-lg-2 col-xl-2 mx-auto">
<div class="form-group">
<button class=" btn btn-gray text-light"
style="background-color: #6c757d;"
type="submit"
value="Send"
onClick="return empty()"
>
Send
</button>
</div>
</div>
</div>
</form>
And, I check the input fields like the following:
<script type="text/javascript">
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = yyyy + '-' + mm + '-' + dd;
console.log('today: ', today);
$('#submit_quote').submit(function()
{
var dateControl = document.querySelector('.input_date');
alert('input date: ', dateControl);
if (jQuery("#cke_1_contents iframe").contents().find("body").text() === "" || dateControl <= today) {
alert('Please enter your message or a correct date.');
return false;
}
});
</script>
How to prevent a user from submitting dates before today or an empty date?
If you compare 2 strings, you are comparing ascii code.
console.log(typeof dateControl); // String
console.log(typeof today); // String
var startOfToday = new Date(today).getTime();
console.log(typeof startOfToday); // Number
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.