Knockout check

I have an asp.net mvc3 project where I am doing bulk editing on a table with a knockout binding. I want to do checks as required and numerical checks when saving data. Is there an easier way to do a knockout check. PS: I do not use forms.

+44
validation knockout-validation
Jan 25 2018-12-12T00: 00Z
source share
3 answers

Check out Knockout-Validation , which carefully configures and uses what is described in the documentation by knockout . Q: Live Example 1: Forcing a Number

You can see it live in Fiddle

UPDATE : the script has been updated to use the latest versions of KO 2.0.3 and ko.validation 1.0.2 using CDN URLs for cloud computing

To install ko.validation:

ko.validation.rules.pattern.message = 'Invalid.'; ko.validation.configure({ registerExtenders: true, messagesOnModified: true, insertMessages: true, parseInputAttributes: true, messageTemplate: null }); 

To configure validation rules, use extensions. For example:

 var viewModel = { firstName: ko.observable().extend({ minLength: 2, maxLength: 10 }), lastName: ko.observable().extend({ required: true }), emailAddress: ko.observable().extend({ // custom message required: { message: 'Please supply your email address.' } }) }; 
+81
Mar 05 2018-12-12T00:
source share

If you do not want to use the KnockoutValidation library, you can write your own. Here is an example of a required field.

Add a javascript class with all the extensions or KO extensions and add the following:

 ko.extenders.required = function (target, overrideMessage) { //add some sub-observables to our observable target.hasError = ko.observable(); target.validationMessage = ko.observable(); //define a function to do validation function validate(newValue) { target.hasError(newValue ? false : true); target.validationMessage(newValue ? "" : overrideMessage || "This field is required"); } //initial validation validate(target()); //validate whenever the value changes target.subscribe(validate); //return the original observable return target; }; 

Then in your view, the Model expands the visibility as follows:

 self.dateOfPayment: ko.observable().extend({ required: "" }), 

There are a number of examples on the Internet in this style of validation.

+5
May 12 '15 at 10:01
source share

Checking Knockout.js is convenient, but it is not reliable. You always need to create server-side validation replication. In your case (when you use knockout.js) you send JSON data to the server and asynchronously, so you can make the user think that he sees the client-side check, but in fact it will be an asynchronous server-side check.

Take a look here for an example upida.cloudapp.net:8080/org.upida.example.knockout/order/create?clientId=1 This is the link "Create order". Try clicking save and play with the products. This example is executed using the upida library (there is a version of spring mvc and asp.net mvc of this library) from codeplex.

-21
Aug 30 '13 at 0:04
source share



All Articles