Click here to Skip to main content
15,891,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

i am trying to add client side validation to input fields in mvc5 application, but it does not seem to validate, i am using database first workflow, i have created the metadata class and partial class that inherits the model class in the Model folder. Below is my code:

C#
[MetadataType(typeof(LoginAccountMetaData))]
    public partial class LoginAccountMetaData
    {
    }



public partial class LoginAccountMetaData
    {
        [Required]
        [Display(Name = "UserName")]
        public string userName { get; set; }
        [Required]
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        public string password { get; set; }
        public Nullable<int> userRole { get; set; }
    }



The username and password fields are not validating when the application is run! is there anything i am missing? Thanks in advance.
Posted
Comments
[no name] 17-Oct-14 23:41pm    
Have you used validation to be active in your view page ?? Please post your view page code. You have to use JavaScript code also. Post your code. I will give you the solution.
Uwakpeter 18-Oct-14 6:30am    
My View code:

@model WebApplication.LoginAccount

@{
ViewBag.Title = "Login";
Layout = "~/Views/Shared/SiteMaster.cshtml";
}
<div id="container">
<div class="container">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">

Login




@Html.ValidationSummary(true)

<div class="form-group">
@Html.LabelFor(model => model.userName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.userName)
@Html.ValidationMessageFor(model => model.userName)
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.password, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.password)
@Html.ValidationMessageFor(model => model.password)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
@Html.ActionLink("Sign Up", "SignUp")
</div>
</div>
</div>
}
</div>
</div>
[no name] 18-Oct-14 12:44pm    
Instead of editor for try using textboxfor for the html helper.
Uwakpeter 19-Oct-14 3:01am    
Just tried that, but it still not validating! Any further assistance will be appreciated.

check answer here[^]
validation in db first[^]
Please these two links, you will surely get your answer.
The issue is with the model, not the view.
Check and try.
C#
[HttpPost]
      public ActionResult Login(LoginViewModel viewModel)
      {
          if (ModelState.IsValid)
          {
              return RedirectToAction("Index", "Home");
          }
          if (string.IsNullOrEmpty(viewModel.UserName))
          {
              ModelState.AddModelError("UserName", "Name is required");
          }

          //else you can also use regular expression check here. like
          if (!string.IsNullOrEmpty(model.Email))
          {
              string emailRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                                       @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                                          @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
              Regex re = new Regex(emailRegex);
              if (!re.IsMatch(model.Email))
              {
                  ModelState.AddModelError("Email", "Email is not valid");
              }
          }
          else
          {
              ModelState.AddModelError("Email", "Email is required");
          }
          return View(viewModel);

      }

ASP.NET MVC Client Side Validation[^]

Post back ur comments with queries if any.
Thanks
 
Share this answer
 
v3
Comments
Uwakpeter 19-Oct-14 11:19am    
Have tried that also, the page still refreshes, meaning not validating, don't know what else to do!
[no name] 19-Oct-14 12:59pm    
Please check the updated solution. I am quite sure this should work out.
All the best Thanks
Uwakpeter 19-Oct-14 16:55pm    
Yes this will work, but it going to trigger on server side, it will cause the page to refresh, I needed to implement a client side validation, tried using reverse engineering tool, this also works, but it a long process to properly implement, I really appreciated your much contribution and assistance, thanks. Will still appreciate any further assistance.
[no name] 19-Oct-14 23:03pm    
You should have told you need client side validation. Ok I have given one link in the updated solution of mine. Please check if that solves your purpose. Else I will be giving you codes examples to check on client side. Thank you.
using data annotations are server side validations,
client side validation by using jquery ,we can per form the client side validations
 
Share this answer
 
Comments
Uwakpeter 17-Oct-14 16:00pm    
I don't know jquery that much, I want to be able to use server side data anotation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900