Search code examples
popupintegerasp.net-mvc-5asp.net-mvc-validationasp.net-mvc-views

MVC 5 integer validation when entering a non integer


I am coding a MVC 5 internet application, and have a question in regards to validating a integer value in a view.

I have a model with the following variable:

public int integerValue { get; set; }

If I enter a string value in the view for this model, I am getting an alert pop up that says:

Please enter a number.

This is not a normal validation message, and instead appears to be related to the browser.

My question is this: Is it possible to override this alert pop up to instead have the usual validation message beneath the input field?

Thanks in advance.


Solution

  • That will be because Mvc has output an editor with input[type=number].

    If you can live without the html5 spinner controls, you can try adding

     [DataType(DataType = DataType.Text)] 
    

    to the property in question.

    Or you can try

     @Html.EditorFor(m => m.{YourProperty}, new { htmlAttributes= new{ @type="text"}})