gmang
gmang

Reputation: 435

ASP.NET MVC Html.textbox - How to validate numbers only?

With the HTML helper, how would you enforce number only without submitting? I know it was done with regular expression if you had a textbox in classic ASP.NET

<%= Html.TextBox("txtYearOfWork",
                 String.Empty, 
                 new { maxlength = 4, size="5", autocomplete = "off" }) %>

Upvotes: 2

Views: 26487

Answers (2)

Andrew Cox
Andrew Cox

Reputation: 10998

Another option would be to use the jquery Validation Plugin.

This would allow you to just add the class "required digits" and it would do the validation magic for you.

Upvotes: 1

Andrew Lewis
Andrew Lewis

Reputation: 5254

If the data type on the model is numeric (int, etc) you can just use an attribute, and turn on client validation. More details here.

Upvotes: 6

Related Questions