my code is:
@Html.TextBoxFor(model => model.Phone, true, null, new { @placeholder = "Enter Phone Number *", @minlength = "10", @maxlength = "12", @autocomplete = "off", @data_rule_number = true, @data_msg_number = "*invalid phone number", @class = "form-control" })
here I have to validate that the user should enter number starting with digits (6/7/8/9). how to achieve that?
CodePudding user response:
You can use the pattern for 12 digits that's shown in below:
pattern = @"^(6|7|8|9)([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])$"
CodePudding user response:
You can add the pattern field:
@Html.TextBoxFor(model => model.Phone, true, null, new { pattern = @"^([6-9][0-9]{9,11})$", @placeholder = "Enter Phone Number *", @minlength = "10", @maxlength = "12", @autocomplete = "off", @data_rule_number = true, @data_msg_number = "*invalid phone number", @class = "form-control" })
The pattern string ^([6-9][0-9]{9,11})$
means the first number can be 6 to 9, then users can use 0 to 9 to fill the remain numbers, and the length of remain numbers is from 9 to 11.
CodePudding user response:
Based on Removable's answer,in .net core
You could add the [RegularExpression(@"^[6-9][0~9]{9,11}$", ErrorMessage = "CusRegularErrorMessage")]
Attribute on your Phone Property of the model for both clientside validation and serverside validation
You could Check this document for more details:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0#built-in-attributes