2

I have added a text box to a simple form in ASP.NET MVC and I want a client-side 'required' validation for this.

I know I can do this using a strongly typed model view but I would like to do it manually in this case. Is there a simple way to perform this?

I tried setting the Model/property name of the Html.ValidationMessage helper to the input name but this didnt work:

@Html.TextBox("emailStr" )
@Html.ValidationMessage("emailStr","* Required")
1
  • The "simple" way of doing it would be to write javascript to do validation on form submit, return false if it failed and add appropriate text and css classes Commented Sep 23, 2011 at 11:34

2 Answers 2

2

Assuming you use default jQuery validation plugin, you can use Rules.Add method on client side for this

$("#emailStr").rules("add", {
 required: true,
 messages: {
   required: "* Required",
 }
});

Also, do not forget to include jquery.validate.min.js

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

Comments

0

Not sure why you would want to do it manually - but I don't think you can use @Html.ValidationMessage unless you use a TextBoxFor. You can't use the TextBoxFor unless you have a model to work with inside the view.

You could write some javascript/jquery to find the textbox and make sure it's not empty, and if it is, unhide an element with the validation message in it.

Comments

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.