Answer the question
In order to leave comments, you need to log in
How to prevent DatePicker from passing empty value to controller?
Good afternoon! Please tell me, I use the DatePicker found on the Internet on the page.
He works. The selected date is sent to the controller.
But it also passes the null value :(
Is it possible to use the java script to remove the ability to pass an empty value, or only to fence towns in the controller?
How is it right?
Here is the code:
@using (Html.BeginForm("Index", "Work", FormMethod.Post))
{
@Html.TextBox("SelectedDate", "", new { @readonly = "readonly" })
<input type="submit" value="Выбрать" />
}
<!-- Bootstrap DatePicker -->
<script type="text/javascript">
$(function () {
$('#SelectedDate').datepicker({
changeMonth: true,
changeYear: false,
format: "dd/mm/yyyy",
language: 'ru'
});
});
</script>
@if (ViewBag.Message != null)
{
<script type="text/javascript">
$(function () {
alert("@ViewBag.Message");
});
if ($("#SelectedDate").datepicker("SelectedDate") === null) {
alert("no date selected");
}
</script>
}
Answer the question
In order to leave comments, you need to log in
You can check the imput itself:
if($("#SelectedDate").val() == "") {
alert("no date selected");
}
if($("#SelectedDate").datepicker("getDate") === null) {
alert("no date selected");
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question