1

I have a registration form and a login form for users. I want validation on the Registration form obviously, but I do not want it on the login form. I've noticed it validates things with JS on the login form. I.E it doesn't even try to submit the form until the user types in a password of length over 5 (I.E what I set in the validation). How do I disable this?

Login form:

@using (Html.BeginForm("Login", "Users", FormMethod.Post, new { @class = "well"})) {

        <legend>Login</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Email, new { @placeholder = "Email" })
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Password, new { @placeholder = "Password" })
        </div>

        <label class="checkbox">
            @Html.CheckBoxFor(model => model.HasRememberMeOn) Remember me
        </label>


        if (ViewData["ErrorMessage"] != null)
        {
                <p>@ViewData["ErrorMessage"]</p>
        }
        <p>
            <input type="submit" value="Login" />
        </p>

}

Model:

[Required]
        [StringLength(20, MinimumLength=5)]
        [Username(ErrorMessage="Username must: Contain at least one letter, no spaces, no special characters")]
        public string Username { get; set; }

        [NotMapped]
        [StringLength(25, MinimumLength = 5)]
        public string Password { get; set; }

        [Required(ErrorMessage="Password is required")]
        public string HashedPassword { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 3)]
        public string Email { get; set; }

        [NotMapped]
        [Compare("Email", ErrorMessage = "Your emails don't match")]
        [DisplayName("Confirm email")]
        public string Email2 { get; set; }

        public Boolean IsActivated { get; set; }

        [DisplayName("Date joined")]
        public DateTime DateCreated { get; set; }

        [Required]
        [StringLength(50, MinimumLength = 2)]
        [DisplayName("Real name")]
        public string Name { get; set; }
1
  • Did my answer help you btw? Commented Feb 26, 2013 at 1:49

1 Answer 1

4

The correct solution would be using another model for the login form without the Required attributes etc. And you need just the username and password for login, so it wouldn't be a good idea to use the registration model anyway.

Sign up to request clarification or add additional context in comments.

2 Comments

+1, why would you use the required attribute, when you are against it. Make it simple and just remove them when you don't need it. People work hard to make sure that the validation stuff would be fool proof.
If you write +1, where is your +1? :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.