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

  • SEARCH
  • Home
  • 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 9220531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:22:15+00:00 2026-06-18T03:22:15+00:00

It may be the easiest question but I am not able to resolve this.

  • 0

It may be the easiest question but I am not able to resolve this. The validation on my page is not working. Everytime I am submitting the page leaving all the input fields blank, an alert is generated saying ‘Failed’. And if I enter all the fields with some value data is successfully submitted.

Here is my HTML :

@{
ViewBag.Title = "Exercise10";
}
<html>
<head>

<script src="../../Scripts/jquery-1.6.2.js" type="text/javascript"></script>
<script src="../../Scripts/knockout-2.2.1.js" type="text/javascript"></script>
<script src="../../Scripts/knockout.mapping-latest.js" type="text/javascript"></script>
<script src="../../Scripts/json2.js" type="text/javascript"></script>
<link href="../../Content/ExerciseStyle.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/Popup.js" type="text/javascript"></script>


<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-    ui.css" />
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<script src="../../Scripts/DatePicker.js" type="text/javascript"></script>
<script src="../../Scripts/knockout.validation.js" type="text/javascript"></script>
<script src="../../Scripts/knockout-validator.js" type="text/javascript"></script>
<script src="../../Scripts/knockout-validator-extensions.js" type="text/javascript"></script>
<link href="../../Scripts/extensions.css" rel="stylesheet" type="text/css" />

</head>
<body>
<form action="" method="post">
<div id="MainArea">
    <table id="tbllist" align="center" style="margin-left: 15px; width: 96%; margin-      right: 15px;">
        <tr>
            <th colspan="3" align="left">
                <div id="title_p">
                    Enter Following Entries</div>
            </th>
        </tr>
        <tr>
            <td align="right" style="width: 40%;">
                <b>Name :</b>
            </td>
            <td align="left" style="width: 17%;">
                <input data-bind="value: EmployeeName" placeholder="Employee Name" class="txt"
                    type="text" />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>Emp# :</b>
            </td>
            <td align="left">
                <input data-bind="value: EmployeeCode" placeholder="Employee Code" style="width: 200px;"
                    type="text" />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>Date of Birth :</b>
            </td>
            <td align="left">
                <input data-bind="value: Dob" id="datepicker" placeholder="Date of Birth" style="width: 200px;"
                    type="text" /><span>(dd/mm/yyyy)</span>
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>Age (18-60):</b>
            </td>
            <td align="left">
                <input data-bind="value: Age" style="width: 200px;" placeholder="Age Range (18-60)"
                    type="number" min="18" max="60" />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>Contact Number :</b>
            </td>
            <td align="left">
                <input data-bind="value: ContactNumber" placeholder="Contact Number" style="width: 200px;"
                    type="text" />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>Email :</b>
            </td>
            <td align="left">
                <input data-bind="value: EmailID" placeholder="Email ID" style="width: 200px;"
                    type="email" />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>Address :</b>
            </td>
            <td align="left">
                <input data-bind="value: Address" placeholder="Address" style="width: 200px;"
                    type="text" />
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>City :</b>
            </td>
            <td align="left">
                <select data-bind="value: City" style="width: 200px;">
                    <option value="Noida">New Delhi</option>
                    <option value="Noida">Noida</option>
                    <option value="Noida">Mumbai</option>
                </select>
            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>Marital Status :</b>
            </td>
            <td align="left">
                <input data-bind="checked: MaritalStatus" checked="checked" name="rdb" type="radio" /><span>UnMarried</span>
                <input data-bind="checked: MaritalStatus" checked="checked" name="rdb" type="radio" checked="checked" /><span>Married</span>

            </td>
            <td align="left">
            </td>
        </tr>
        <tr>
            <td align="right">
                <b>Any Employee Reference :</b>
            </td>
            <td align="left">
                <input data-bind="checked: Is_Reference" type="checkbox" />yes
            </td>
            <td align="left">
            </td>
        </tr>
    </table>
    <table style="width: 99%; margin-right: 20px; padding: 5px;">
            <tr align="right">
                <td>
                    <button data-bind="click :$root.save" class="button">Save</button>
                    <input type="button" id="btnCancel" class="button" value="Cancel" onclick="JavaScript:closePopup();" />
                </td>
            </tr>
        </table>
</div>
</form>

And My View Model (continued from the above):

<script type="text/javascript">



//....................................................//
    var EmpViewModel = function () {


        //Make the self as 'this' reference
        var self = this;
        //Declare observable which will be bind with UI 
        self.EmployeeCode = ko.observable("").extend({ required: true });
        self.EmployeeName = ko.observable("").extend({ required: { message: 'Please supply your Name.'} });
        self.Dob = ko.observable("");
        self.Age = ko.observable("").extend({number :true});
        self.ContactNumber = ko.observable("");
        self.EmailID = ko.observable("");
        self.Address = ko.observable("");
        self.MaritalStatus = ko.observable("");
        self.City = ko.observable("");
        self.Is_Reference = ko.observable("");

        //The Object which stored data entered in the observables
        var EmpData = {
            EmpCode: self.EmployeeCode,
            EmpName: self.EmployeeName,
            Dob: self.Dob,
            Age: self.Age,
            ContactNumber: self.ContactNumber,
            MaritalStatus: self.MaritalStatus,
            EmailID: self.EmailID,
            Address: self.Address,
            City: self.City,
            Is_Reference: self.Is_Reference
        };

        //Declare an ObservableArray for Storing the JSON Response
        self.Employees = ko.observableArray([]);

        //Function to perform POST (insert Employee) operation
        self.save = function () {
            //Ajax call to Insert the Employee
            $.ajax({
                type: "POST",
                url: "/Exercise/Save/",
                data: ko.toJSON(this), //Convert the Observable Data into JSON
                contentType: "application/json",
                success: function (data) {
                    alert(data);
                },
                error: function () {
                    alert("Failed");
                }
            });
            //Ends Here
        };
    }

    ko.applyBindings(new EmpViewModel());
</script>

  • 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-06-18T03:22:16+00:00Added an answer on June 18, 2026 at 3:22 am

    Knockout-validation shows validation messages only if the fields are modified. Therefore you should check on submit if all fields are valid and show all errors if not.

     self.errors = ko.validation.group(this, { deep: true, observable: false });
    
      //Function to perform POST (insert Employee) operation
            self.save = function () {
                // check if valid
                if(self.errors().length > 0) {
                    self.errors.showAllMessages();
                    return;
                }
                //Ajax call to Insert the Employee
                $.ajax({
                    type: "POST",
                    url: "/Exercise/Save/",
                    data: ko.toJSON(this), //Convert the Observable Data into JSON
                    contentType: "application/json",
                    success: function (data) {
                        alert(data);
                    },
                    error: function () {
                        alert("Failed");
                    }
                });
                //Ends Here
     };
    

    I have created a fiddle to show that: http://jsfiddle.net/delixfe/tSzYf/2/

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

Sidebar

Related Questions

This may not sound like a coherient question, but I need to find some
This question may be seen as subjective, but I'd like to ask SO users
This may be a stupid question, but I'm interested in the performance of using
I realize that this question may appear to be a duplicate, but none of
This may be a dumb question, but in MVC the view doesn't know about
The easiest way to explain my question may be with an example, so let
OK, this may sound like an odd question so first let me lay out
This user asked a similar question but had no viable solution in the answers,
Not sure the most effient way to do this. Bash seems the easiest, I
Quick question, of which the quickest and easiest answer may well be to rearrange

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.