Razim Khan
Razim Khan

Reputation: 31

Validating Kendo Numeric textbox to Accept max 15 digit min 1

i have a kendo text box , which i want to validate to mobile number

  @(Html.Kendo().TextBox().Name("MobileNo")
        .HtmlAttributes(new { @class = "form-control", style = "width:100%;", placeholder = "Enter Mobile Number  ", required = "required",validationmessage = "Enter {0}", data_required_msg = "Enter Mobile Number" }))

Upvotes: 0

Views: 1190

Answers (1)

user3559349
user3559349

Reputation:

Use a RegularExpressionAttribute in conjunction with a RequiredAttribute on you property.

[Required(ErrorMessage = "Please enter a mobile phone")]
[RegularExpression(@"^\d{1,15}$", ErrorMessage = "Please enter between 1 and 15 numbers")]
public string MobileNo { get; set; }

and in the view

@(Html.Kendo().TextBox().Name("MobileNo")
    .HtmlAttributes(new { @class = "form-control", style = "width:100%;", placeholder = "Enter Mobile Number" }))
@Html.ValidationMessageFor(m => m.MobileNo)

Note that you should remove the required, validationmessage and data_required_msg attributes.

This will now give you both client side and (more importantly) server side validation.

Upvotes: 1

Related Questions