Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
61 views
in Technique[技术] by (71.8m points)

c# - ASP.NET Validation Attribute

In my model RegisterViewModel on Password property, I made two validation attributes. In this case, I use the usual MVC and jQuery validations. The problem is that the validation message shows the only

[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[#$^+=!*()@%&]).{0,}$", ErrorMessage = CommonConstants.CREATE_USER_PASSWORD_ERROR)]

I want if the password is not between 6-15 characters to show message from this attribute

[StringLength(15, ErrorMessage = "The password must be at least {2} and at max {1} characters long.", MinimumLength = 6)]

Password property:

PASSOWRD WITH UNDER 6 CHARACTERS PASSWORD >15 CHARACTERS

    [Required(ErrorMessage = "Password field is required")]
            [StringLength(15, ErrorMessage = "The password must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[#$^+=!*()@%&]).{6,15}$",ErrorMessage = CommonConstants.CREATE_USER_PASSWORD_ERROR)]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string Password { get; set; }

This is the view:

@using (Html.BeginForm("Create", "Users", FormMethod.Post))
{
    @Html.AntiForgeryToken();
<div class="form-horizontal">
    <h4>Contacts</h4>
    <hr />

    @Html.HiddenFor(x => Model.RegisterViewModel.OrganizationId)

    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <div class="col-md-2 control-label">
            @Html.LabelFor(x => Model.RegisterViewModel.Email, htmlAttributes: new { @class = "control-label" })
        </div>
        <div class="col-md-10">
            @Html.TextBoxFor(x => Model.RegisterViewModel.Email, htmlAttributes: new { @class = "form-control", @readonly="readonly" })
            @Html.ValidationMessageFor(x => x.RegisterViewModel.Email, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-2 control-label">
            @Html.LabelFor(x => Model.RegisterViewModel.FirstName, htmlAttributes: new { @class = "control-label" })
        </div>
        <div class="col-md-10">
            @Html.TextBoxFor(x => Model.RegisterViewModel.FirstName, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.RegisterViewModel.FirstName, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-2 control-label">
            @Html.LabelFor(x => Model.RegisterViewModel.LastName, htmlAttributes: new { @class = "control-label" })
        </div>
        <div class="col-md-10">
            @Html.TextBoxFor(x => Model.RegisterViewModel.LastName, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.RegisterViewModel.LastName, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-2 control-label">
            @Html.LabelFor(x => Model.RegisterViewModel.Mobile, htmlAttributes: new { @class = "control-label" })
        </div>
        <div class="col-md-10">
            @Html.TextBoxFor(x => Model.RegisterViewModel.Mobile, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.RegisterViewModel.Mobile, "", new { @class = "text-danger" })
        </div>
    </div>
    @*<div class="form-group">
            <div class="col-md-2 control-label">
                @Html.LabelFor(x => Model.RegisterViewModel.Role, htmlAttributes: new { @class = "control-label" })
            </div>
            <div class="col-md-10">
                @Html.DropDownListFor(x => Model.RegisterViewModel.Role, new SelectList(Model.RegisterViewModel.Roles, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "Role" })
                @Html.ValidationMessageFor(x => x.RegisterViewModel.Role, "", new { @class = "text-danger" })
            </div>
        </div>*@
    <div class="form-group">
        <div class="col-md-2 control-label">
            @Html.LabelFor(x => Model.RegisterViewModel.Department, htmlAttributes: new { @class = "control-label" })
        </div>
        <div class="col-md-10">
            @Html.TextBoxFor(x => Model.RegisterViewModel.Department, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(x => x.RegisterViewModel.Department, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-2 control-label">
            @Html.LabelFor(x => Model.RegisterViewModel.Password, htmlAttributes: new { @class = "control-label" })
        </div>
        <div class="col-md-10">
            @Html.TextBoxFor(x => Model.RegisterViewModel.Password, htmlAttributes: new { @class = "form-control", placeholder="Password", @type = "password" })
            @Html.ValidationMessageFor(x => x.RegisterViewModel.Password, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-2 control-label">
            @Html.LabelFor(x => Model.RegisterViewModel.ConfirmPassword, htmlAttributes: new { @class = "control-label" })
        </div>
        <div class="col-md-10">
            @Html.TextBoxFor(x => Model.RegisterViewModel.ConfirmPassword, htmlAttributes: new { @class = "form-control", placeholder="Confirm Password", @type = "password" })
            @Html.ValidationMessageFor(x => x.RegisterViewModel.ConfirmPassword, "", new { @class = "text-danger" })
        </div>
    </div>

</div>

<br />

}

question from:https://stackoverflow.com/questions/65897537/asp-net-validation-attribute

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Possibly use the RegEx Library website to research and help identify some examples; https://regexlib.com/Search.aspx?k=6-15%20characters

There are so many variations and you can tweak them as needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...