Allow Only Numbers in TextBox in .Net MVC

When developing ASP.NET MVC applications, it's common to encounter situations where you need to restrict user input in a TextBox to numeric values only. Whether you're building a financial application, a calculator, or any form requiring numerical input, ensuring data integrity through input validation is crucial. In this blog post, we'll explore multiple methods to achieve this goal in MVC, including code examples for both the server-side and client-side implementations.

Method 1: Regular Expression Validation

Regular expressions provide a flexible and powerful way to validate and restrict input. In this method, we'll use a regular expression pattern to ensure that only numeric values are allowed. Let's start with the model class:

public class MyModel
    [RegularExpression(@"^\d+$", ErrorMessage = "Please enter only numeric values.")]
    public int NumberField { get; set; }

In this example, the RegularExpression attribute is applied to the NumberField property of the MyModel class. The regular expression pattern @"^\d+$" specifies that only numeric values should be accepted.

Next, let's see how to render the corresponding TextBox in the view:

@model MyModel

@using (Html.BeginForm("SubmitData", "Home", FormMethod.Post))
@Html.LabelFor(model => model.NumberField) @Html.TextBoxFor(model => model.NumberField, new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.NumberField)

Here, the TextBoxFor method is used to generate the input field for the NumberField property. The model binder and regular expression validation will ensure that only numeric values are accepted in this TextBox.

Method 2: Using JavaScript

Adding client-side validation enhances user experience by providing immediate feedback without requiring a server round-trip. JavaScript can be used to validate input before submitting the form. Let's see an example using jQuery:

@model MyModel

@using (Html.BeginForm("SubmitData", "Home", FormMethod.Post))
@Html.LabelFor(model => model.NumberField) @Html.TextBoxFor(model => model.NumberField, new { @class = "form-control", onkeypress = "return isNumberKey(event)" }) @Html.ValidationMessageFor(model => model.NumberField)

Method 3: HTML5 Input Type

HTML5 introduced several new input types, including number, which restricts input to numeric values. By using the number input type, modern browsers will automatically enforce the requirement. Let's update the view to utilize this feature:

@Html.TextBoxFor(model => model.NumberField, new { @class = "form-control", type = "number" })

By setting the type attribute to number, the browser will handle the validation and enforce numeric input for you.

In this blog post, we explored multiple methods to restrict user input to only numeric values in an MVC application. Whether you prefer using regular expressions, JavaScript validation, HTML5 input types.

Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

8 + 2 =

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice