Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 262075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:29:07+00:00 2026-05-11T22:29:07+00:00

I have been working on this problem for 2 days now and it’s an

  • 0

I have been working on this problem for 2 days now and it’s an easy problem and I just can’t see the error in the code, even after comparing it with other projects where this works.

Could you help out?

I’m working on the account section of my website using ASP.NET Membership and the account controller class that is generated with ASP.NET MVC.

However when registering and the user leaves some fields blank the entered text dosn’t show up when the page is redisplayed.

Here is the register post action

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Register(FormCollection formValues)
    {

        ViewData["PasswordLength"] = MembershipService.MinPasswordLength;

        if (ValidateRegistration(formValues))
        {

            // Attempt to register the user
            MembershipCreateStatus createStatus = MembershipService.CreateUser(formValues["username"], formValues["password"], formValues["email"]);

            if (createStatus == MembershipCreateStatus.Success)
            {
                FormsAuth.SignIn(formValues["username"], false /* createPersistentCookie */);

                return RedirectToAction("CreateCustomer", "Account");
            }
            else
            {
                ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus));
            }
        }

        // If we got this far, something failed, redisplay form
        return View();
    }

And the ValidateRegistration function

 private bool ValidateRegistration(FormCollection formValues)
    {
        if (String.IsNullOrEmpty(formValues["userName"]))
        {
            ModelState.AddModelError("username", "You must specify a username.");
        }
        if (String.IsNullOrEmpty(formValues["email"]))
        {
            ModelState.AddModelError("email", "You must specify an email address.");

        }
        if (formValues["password"] == null || formValues["password"].Length < MembershipService.MinPasswordLength)
        {
            ModelState.AddModelError("password",
                String.Format(CultureInfo.CurrentCulture,
                     "You must specify a password of {0} or more characters.",
                     MembershipService.MinPasswordLength));

        }
        if (!String.Equals(formValues["password"], formValues["confirmPassword"], StringComparison.Ordinal))
        {
            ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
        }
        if (!String.Equals(formValues["email"], formValues["confirmEmail"], StringComparison.Ordinal))
        {
            ModelState.AddModelError("confirmEmail", "The email and confirmation email addresses do not match.");
        }
        return ModelState.IsValid;
    }

And just in case here is the Register view

” %>

Register

Create a New Account

Use the form below to create a new account.

Passwords are required to be a minimum of <%=Html.Encode(ViewData[“PasswordLength”])%> characters in length.

<%= Html.ValidationSummary() %>

<% using (Html.BeginForm()) { %>

Username:
<%= Html.TextBox(“username”)%>

                <%= Html.ValidationMessage("username") %>
            </td>
            </tr>

            <tr>
            <td>Email:</td>
            <td><%= Html.TextBox("email") %>
                <%= Html.ValidationMessage("email") %>
            </td>
            </tr>
            <tr>
            <td>Confirm Email:</td>
            <td>
                <%= Html.TextBox("confirmEmail") %>
                <%= Html.ValidationMessage("confirmEmail") %>
            </td>
            </tr>
            <tr>
            <td>Password:</td>
            <td>
                <%= Html.Password("password") %>
                <%= Html.ValidationMessage("password") %>
            </td>
            </tr>
            <tr>
            <td>Confirm password:</td>
            <td>
                <%= Html.Password("confirmPassword") %>
                <%= Html.ValidationMessage("confirmPassword") %>
            </td>
            </tr>
            <tr>

            <td></td>
            <td>
                <input type="submit" value="Register" />
            </td>
            </table>
    </div>


<% } %>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T22:29:07+00:00Added an answer on May 11, 2026 at 10:29 pm

    You need to call Model.SetModelValue() because all you’ve done is tell the form that it has errors and what fields those errors are on BUT you haven’t told it what the erroneous data is.

    Try something like this:

    private bool ValidateRegistration(FormCollection formValues)
    {
        if (String.IsNullOrEmpty(formValues["userName"]))
        {
            ModelState.AddModelError("userName", "You must specify a username.");
        }
    
        if (String.IsNullOrEmpty(formValues["email"]))
        {
            ModelState.AddModelError("email", "You must specify an email address.");
        }
    
        if (formValues["password"] == null || formValues["password"].Length < MembershipService.MinPasswordLength)
        {
            ModelState.AddModelError("password",
                String.Format(CultureInfo.CurrentCulture,
                     "You must specify a password of {0} or more characters.",
                     MembershipService.MinPasswordLength));
        }
    
        if (!String.Equals(formValues["password"], formValues["confirmPassword"], StringComparison.Ordinal))
        {
            ModelState.AddModelError("confirmPassword", "The new password and confirmation password do not match.");
        }
    
        if (!String.Equals(formValues["email"], formValues["confirmEmail"], StringComparison.Ordinal))
        {
            ModelState.AddModelError("confirmEmail", "The email and confirmation email addresses do not match.");
        }
    
        if (!ModelState.IsValid)
        {
            IDictionary<string, ValueProviderResult> valueProvider = formvalues.ToValueProvider();
            ModelState.SetModelValue("userName", valueProvider["userName"]);
            ModelState.SetModelValue("email", valueProvider["email"]);
            ModelState.SetModelValue("confirmEmail", valueProvider["confirmEmail"]);
            ModelState.SetModelValue("password", valueProvider["password"]);
            ModelState.SetModelValue("confirmPassword", valueProvider["confirmPassword"]);
        }
    
        return ModelState.IsValid;
    }
    

    HTHs,
    Charles

    Ps. You should trim any input you require or are going to validate it’s length… I’ve fallen into a similar trap before 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on this for a few days now, and I've found several
I've been batting this problem around in my head for a few days now
I have been working on this for the greater part of the day and
I have been working on Flex for last couple of months and as this
I have this classic ASP site which has been working fine until we updated
I have been working on some legacy C++ code that uses variable length structures
I have been working as a native C++ programmer for last few years. Now
I have a website that i've been working on for about a year now.
I'm hoping someone can help me as I've been stuck on this problem for
I have been working on a web services related project for about the last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.