2

I created database and add a table using code first workflow and then created ADO.NET Entity data model, when I do that it automatically generated model class for the table in that database,

within that model class its defined Password Field but not Confirm Password field since I don't want store both,

HERE code snippet in my Model class

    [Required(ErrorMessage = "Please provide Password", AllowEmptyStrings = false)]
    [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]
    [StringLength(255, MinimumLength = 5, ErrorMessage = "Password must be 5 char long.")]
    public string Password { get; set; }

    public string PasswordSalt { get; set; }

to compare above two field I just try to use java script front end validation

I just want to know how to compare Password Field and Confirm Password Field using JavaScript in cshtml I already tried many ways to do this ,but still cannot figure it out ,

This is the code snippet I added overs the cshtml file

 <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript">
    $(function isPasswordSame()  {
        var password = document.getElementById('password');
        var confirmPassword = document.getElementById('passsword-confirm');        
        return password === confirmPassword;
    });
    </script>

this is the CSHTML body definition of above model class

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

         <div class="editor-label">
          <label> Confirm Password</label>
        </div>
        <div class="editor-field">
            <input type="password" id="passsword-confirm"/>
        </div>

but this not working properly .looking for good overcome solution or any enhancement , Thanks

HERE the Full CSHTML code

@model  AFFEMS2_HEC.Models.HEI_REG_TABLE
    @{
    ViewBag.Title = "HEI_Registration";
    Layout = "~/Views/Shared/Login_Layout.cshtml";
     }
    <script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript">
    $(function isPasswordSame()  {
        var password = document.getElementById('password');
        var confirmPassword = document.getElementById('passsword-confirm');

        return password === confirmPassword;
    });

</script>
<script type="text/jscript"></script>
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>User</legend>
        @Html.AntiForgeryToken()
        @if (ViewBag.Message != null)
        {
            <div style="border:solid 1px green">
                @ViewBag.Message
            </div>
        }

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

        <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 id="password" class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>

        <div class="editor-label">

        </div>
        <div id="passsword-confirm" class="editor-field">
             <input type="password" />
        </div>

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

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

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
kez
  • 2,273
  • 9
  • 64
  • 123
  • 1
    A common approach is to create a view model with properties for `Password` and `ConfirmPassword` and then apply the `[Compare]` attribute so you get both client side and server side validation –  Oct 24 '14 at 04:37
  • What do you mean it's not working properly? Also can you post your full html? – JanR Oct 24 '14 at 04:37
  • @StephenMuecke when I define it , I cannot pass values to database since my Table has not field called ConfirmPassword – kez Oct 24 '14 at 04:41
  • Its got nothing to do with the database. A view model represents what you want to display and edit in a view. You initialize a view model, map you data model properties to it, send it to the view, post back the view model, check if its valid, then map the necessary properties to your data model and save it. –  Oct 24 '14 at 04:45
  • @StephenMuecke I'm getting user unhanded exception error , when I define it in model class – kez Oct 24 '14 at 04:59
  • I assume you trying to add the property to your data model. It needs to be a new class, for example `public class RegisterViewModel` –  Oct 24 '14 at 05:03
  • Where were you calling your function? And suggest you put your scripts inside the `@section Scripts` section (jquery first, then jqueryval, then the page specific scripts) –  Oct 24 '14 at 05:30
  • I'm getting following error when I define it " Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details." – kez Oct 24 '14 at 07:49
  • @kez, That error is to do with EF (passing an incorrect model) and nothing to do with creating a view model to edit data. I assume you are not understanding the basic concept. [What is a view model](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc). As far your question goes, you define a function `isPasswordSame()` but don't appear to be calling it. –  Oct 24 '14 at 07:59

0 Answers0