A
A
Andrey2019-10-29 13:12:17
JavaScript
Andrey, 2019-10-29 13:12:17

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>
}


Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Planet_93, 2019-10-29
@andrey71

You can check the imput itself:

if($("#SelectedDate").val() == "") {
  alert("no date selected");
}

Either for the DatePicker use getDate which returns null if nothing is selected:
if($("#SelectedDate").datepicker("getDate") === null) {
  alert("no date selected");
}

R
Roman, 2019-10-29
@yarosroman

www.java2s.com/Code/JavaScript/Form-Control/Usingt...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question