Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating time-only input in asp.net MVC unobtrusive validation

I have two separate fields on the page: one for date and one for time.

This is the model:

[Required]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
public DateTime? StartTime { get; set; }

[Required]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime Date { get; set; }

This is the view:

@Html.TextBoxFor(m => m.Date, "{0:MM/dd/yyyy}", new { type = "text" })
@Html.TextBoxFor(m => m.StartTime, "{0:hh:mm tt}", new { type = "text", id = "timeStart" })

The javascript unobtrusive validation works fine with the Date field however when i enter "11:00 PM" or "11:00 pm" in StartTime the validation shows

"The field StartTime must be a date"

Server side validation works fine with "0:hh:mm tt" it's only the javascript that has a problem. For now i just disabled javascript validation but would like eventually to have it on this page

Can this be done for "time" field?

like image 934
Vitalik Avatar asked Jun 01 '13 12:06

Vitalik


People also ask

How ModelState IsValid works in MVC?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

What is validate input in MVC?

The ValidateInput attribute is used to allow sending the HTML content or codes to the server which, by default, is disabled by ASP.NET MVC to avoid XSS (Cross-Site Scripting) attacks. This attribute is used to enable or disable the request validation. By default, request validation is enabled in ASP.NET MVC.

How many types of validation are there in ASP.NET MVC?

The following three type of validations we can do in ASP.NET MVC web applications: HTML validation / JavaScript validation. ASP.NET MVC Model validation. Database validation.


2 Answers

Honestly the easiest way to achieve this is to use a regular expression validator for it. Here is an example.

[RegularExpression(@"^(0[1-9]|1[0-2]):[0-5][0-9] (am|pm|AM|PM)$", ErrorMessage = "Invalid Time.")]

The unobtrusive validation should work just fine with this expression.

Hope this can help you!

EDIT

I've fixed the regular expression which started throwing errors in the console because of some illegal characters. Also, you will need a string property wrapper for this property or else it will always look for a valid DateTime.

Below is what you should be binding to.

Model:

public DateTime? StartTime { get; set; }

[Required]
[RegularExpression(@"^(0[1-9]|1[0-2]):[0-5][0-9] (am|pm|AM|PM)$", ErrorMessage = "Invalid Time.")]
public string StartTimeValue
{
    get
    {
        return StartTime.HasValue ? StartTime.Value.ToString("hh:mm tt") : string.Empty;
    }

    set
    {
        StartTime = DateTime.Parse(value);
    }
}

View:

@Html.TextBoxFor(m => m.StartTimeValue)
like image 145
technicallyjosh Avatar answered Oct 02 '22 17:10

technicallyjosh


Add DataType.Time attribute to your time field and use EditorFors to remove format duplication:

Model

    [Required]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
    [DataType(DataType.Time)]
    public DateTime? StartTime { get; set; }

    [Required]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime Date { get; set; }

View

    @Html.EditorFor(m => m.Date, new { type = "text" })
    @Html.EditorFor(m => m.StartTime, new { type = "text", id = "timeStart" })
like image 23
Dima Avatar answered Oct 02 '22 16:10

Dima