Confirmation of data Registrations E-mail Password

Asked

Viewed 642 times

1

would like to know the best way to validate E-mail and password, to check if the user has typed the information correctly. Ex:

Enter your e-mail address:

Confirm your e-mail address:

Enter your password:

Confirm your password:

the best way is to validate in the same HTML code or controller? and in case of this information, store in the database also the confirmed data?

Bench:

Email

Confirmaemail

Password

Password confirmation

  • the best way is in the 2, always in front-end and backend..

1 answer

3


ASP.NET MVC has it ready in Model, automatically replicable to Views. Example:

Viewmodel

public class RegisterBindingModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Display(Name = "Confirmar Email")]
    [Compare("Email", ErrorMessage = "E-mail e confirmação não são os mesmos.")]
    public string ConfirmEmail { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "{0} deve ter pelo menos {2} caracteres de comprimento.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Senha")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirmar senha")]
    [Compare("Password", ErrorMessage = "Senha e confirmação não são as mesmas.")]
    public string ConfirmPassword { get; set; }
}

View

@model SeuSistema.ViewModels.RegisterBindingModel

<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h4>Create a new account.</h4>
    <hr />
    @Html.ValidationSummary("", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmEmail, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.ConfirmEmail, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Register" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

and in the case of this information, store in the bank also the confirmed data?

No, you never store confirmations. That’s why the implementation uses Viewmodel, and not Model. In Controller, you will do an additional treatment when saving the user.

  • Can I put the email in one class and the Confirm email in another class? Because in my project, I have a Employee class that Inherits Person, where the Employee would need a Login and Password, but other types who inherit the person just an email to contact

  • So you don’t need any of that. You put the whole creation using one Viewmodel and creates the Model from him.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.