2

I have 3 textboxes ...one for day, second for month and third for year. I want to use mvc validation to check if one of this field is empty and then show *. Is it possible on button submit display just one error message if one of those fields are empty?

<div class="form-group">
    <label for="dateofbirth" class="control-label col-lg-5">
        @Html.Label(@BetXOnline.TranslationProvider.Instance.GetTranslationMessage("BIRTHDATE")):
        @Html.ValidationMessage("*")
    </label>
    <div class="col-lg-2">
        @Html.TextBoxFor(m => m.Register.Day, new { id = "day_birthdate", @class = "form-control" })
    </div>
    <div class="col-lg-2">
        @Html.TextBoxFor(m => m.Register.Month, new { id = "month_birthdate", @class = "form-control" })
    </div>
    <div class="col-lg-3">
        @Html.TextBoxFor(m => m.Register.Year, new { id = "year_birthdate", @class = "form-control" })
    </div>
tereško
  • 58,060
  • 25
  • 98
  • 150
None
  • 8,817
  • 26
  • 96
  • 171
  • Implement IValidateableObject on Register class: http://stackoverflow.com/questions/3400542/how-do-i-use-ivalidatableobject – Kaido May 11 '15 at 14:00

2 Answers2

0

In your module class on the property attached with textbox provide attribute [Required].

than you can use of ValidationSummary which is already available in Asp.net MVC.

if you want to display message just beside file than do use ValidationMessageFor

Check : ASP.NET MVC Client Side Validation

class property definition for this

    [Required(ErrorMessage = "*")]
    public string Name { get; set; }

Example

<div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div> 
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • i dont want to use validation summary because i need to display error message next to label...i have another textboxes which i need to validate and show next to other labels..and if i use like u say i would get 3 error messages – None May 11 '15 at 13:47
  • but i want to check if textbox of day or month or year is empty and then call validationmessagefor... how can i do that?This is just for one field in your example – None May 11 '15 at 13:49
  • @None - you need to attached datanotation to proeprty as i given in my example – Pranay Rana May 11 '15 at 13:50
  • i have 3 field that i need to check... in your example its just one – None May 11 '15 at 13:52
  • @None - so have to do same thing for other two..as simple as that...check the link also given by me – Pranay Rana May 11 '15 at 13:53
  • yes but if i do that i will get 3 different messages for each filed but i want only one if fields are not populated – None May 11 '15 at 13:59
0

You can add the @ValidationMessageFor for all fields under same label.

<label for="dateofbirth" class="control-label col-lg-5">
    @Html.Label(@BetXOnline.TranslationProvider.Instance.GetTranslationMessage("BIRTHDATE"))
    :
    @Html.ValidationMessageFor(m => m.Register.Day,"", new { @class = "text-danger" })
    @Html.ValidationMessageFor(m => m.Register.Month,"", new { @class = "text-danger" })
    @Html.ValidationMessageFor(m => m.Register.Year,"", new { @class = "text-danger" })
</label>
adricadar
  • 9,971
  • 5
  • 33
  • 46