开发者

How can I validate if a user has written in the same two passwords correctly in my view?

Here is my table schema:

How can I validate if a user has written in the same two passwords correctly in my view?

Here is my controller:

public ActionResult Register()
{
    return View();
}

[HttpPost]
public ActionResult Register(FormCollection formValues)
{
    return View();
}

And here is my Register view:

@model LocalizationWebsite.Models.SiteUser

@{
    ViewBag.Title = "Localization | Create Account";
}

<h2>Create Your Account</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <div class="editor-label">
        @Html.LabelFor(model => model.Username)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Username)
        @Html.ValidationMessageFor(model => model.Username)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Password)
    </div>
    <div class="开发者_开发技巧editor-field">
        @Html.EditorFor(model => model.Password)
        @Html.ValidationMessageFor(model => model.Password)
    </div>

    <div class="editor-label">
        @Html.EditorFor(model => model.Password)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Password)
        @Html.ValidationMessageFor(model => model.Password)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.AvatarPath)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.AvatarPath)
        @Html.ValidationMessageFor(model => model.AvatarPath)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

How do you suggest I check if the same password was written in both fields? If possible, I'd like to do this automatically as the user is typing in.

Thank you for the help.


You could decorate your view model properties with the [Compare] validation attribute to ensure that two propery values are matching.


If you want to do it from scratch, I would suggest using a little JQuery or javascript. This will give you control over how to do it "automatically."

But, if you can use the [Compare] attribute on the ViewModel, then this post from Scott Gu will allow you to use client-side validation via the DataAnnotations.


Use the CompareAttribute. Good article: http://davidhayden.com/blog/dave/archive/2011/01/01/CompareAttributeASPNETMVC3.aspx

How can I validate if a user has written in the same two passwords correctly in my view?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜