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)
6
  • Can you show your model? that is where you should put your validation. Commented May 27, 2015 at 12:25
  • 1
    This is about your previous question? Commented May 27, 2015 at 12:27
  • @Donal there is my model... Commented May 27, 2015 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.... Commented May 27, 2015 at 13:42
  • i know that is better but i need to do this with three textboxes Commented May 27, 2015 at 13:49

4 Answers 4

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".

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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

2 Comments

I want only one error if those fields are empty...not three...read my question
@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: stackoverflow.com/questions/8242847/… I hope it helps you.
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.

2 Comments

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
i need only *(only one) if year OR month OR day is empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.