0

I have three textboxes and i need to check if one of field is not enter and display error(just one error). Is this possible with MVC validation or i need javascript validation?

@Html.TextBoxFor(m => m.Register.Day, new { id = "day_birthdate" })
@Html.TextBoxFor(m => m.Register.Month, new { id = "month_birthdate"})
@Html.TextBoxFor(m => m.Register.Year, new { id = "year_birthdate" })

Model:

    public int? Day { get; set; }

    public int? Month { get; set; }

    public int? Year { get; set; }

I dont want to get three different errors...I dont want this

@Html.TextBoxFor(m => m.Register.Day, new { id = "day_birthdate" })
@Html.ValidationMessageFor(m => m.Register.Day)

@Html.TextBoxFor(m => m.Register.Month, new { id = "month_birthdate"})
@Html.ValidationMessageFor(m => m.Register.Month)

@Html.TextBoxFor(m => m.Register.Year, new { id = "year_birthdate" })
@Html.ValidationMessageFor(m => m.Register.Year)
ekad
  • 14,436
  • 26
  • 44
  • 46
None
  • 8,817
  • 26
  • 96
  • 171
  • Can you show your model? that is where you should put your validation. – Donal May 27 '15 at 12:25
  • 1
    This is about your [previous question](http://stackoverflow.com/questions/30169284/mvc-client-side-validation/30169499#30169499)? – adricadar May 27 '15 at 12:27
  • @Donal there is my model... – None May 27 '15 at 12:29
  • Well it seems you really need all those inputs to come up with a Date so it's weird to have things like `nullable` and `[Required]`. Does `DateTime` work better in your model? You can map/validate from whatever input fields on the front end to it.... – EdSF May 27 '15 at 13:42
  • i know that is better but i need to do this with three textboxes – None May 27 '15 at 13:49
  • Yes it's possible, but why would you want to do this? –  May 28 '15 at 00:17

4 Answers4

1

You can also add an error message to the model state like:

ModelState.AddModelError("Day", "Something is wrong with Day");

To bind the above error to a specific property you specify the property name as the first parameter => "Day".

Daniel
  • 150
  • 11
0
in the class model for Register,add
[Required(ErrorMessage = "required")]
public int Day
{
get;
set;
}


     to get [Required(ErrorMessage = "required")] this add namespace 
        using System.ComponentModel.DataAnnotations;  
        in controller,also check if(modelstate.isvalid==true)
shajeer
  • 50
  • 3
0

Model:

[Required(ErrorMessage = "required")]
public int? Day { get; set; }

[Required(ErrorMessage = "required")]
public int? Month { get; set; }

[Required(ErrorMessage = "required")]
public int? Year { get; set; }
Fourat
  • 2,366
  • 4
  • 38
  • 53
  • I want only one error if those fields are empty...not three...read my question – None May 27 '15 at 12:37
  • @None in this case please edit your question and make more clear ! I don't think that what you're asking for is available in .NET MVC, so you should implement a custom validation. See this question: http://stackoverflow.com/questions/8242847/model-validation-asp-net-mvc-3-conditional-required-attribute I hope it helps you. – Fourat May 27 '15 at 20:08
0

First you need to specify in your model which fields are required.

[Required]
public int? Day { get; set; }

[Required]    
public int? Month { get; set; }

[Required]
public int? Year { get; set; }

*you need to import namespace System.ComponentModel.DataAnnotations to get [Required]

then in view you need to add the validation message

@Html.TextBoxFor(m => m.Register.Day, new { id = "day_birthdate" })
@Html.ValidationMessageFor(m => m.Register.Day)

@Html.TextBoxFor(m => m.Register.Month, new { id = "month_birthdate"})
@Html.ValidationMessageFor(m => m.Register.Month)

@Html.TextBoxFor(m => m.Register.Year, new { id = "year_birthdate" })
@Html.ValidationMessageFor(m => m.Register.Year)

Let me know if it works.

Venkata K. C. Tata
  • 5,539
  • 4
  • 22
  • 35
  • it will work its just i want only one error for those three fields and not one error for each field if u know what i mean – None May 27 '15 at 12:44
  • i need only *(only one) if year OR month OR day is empty – None May 27 '15 at 12:51