Chitika

February 4, 2013

Knockout validation: Сomparison of the two fields

I would like to share how I've solved this problem.

The typical example is a simple registration form with the view model:

    var viewModel = ko.validatedObservable({
        userName: ko.observable().extend({ required: true }),
        password: ko.observable().extend({ required: true }),
        passwordConfirm: ko.observable().extend({ required: true })
    });

Then you can easily check is your viewModel valid using:

    viewModel.isValid();

And it works very good. But you don't have validation are your password and password confirmation fields equal.

To do it you have to add these lines:

    viewModel().password.extend({ equal: viewModel().passwordConfirm });
    viewModel().passwordConfirm.extend({ equal: viewModel().password });

After that you will have client notification as well as ability to check is your view model valid in code (as mentioned above).

That's all.
Good luck.

10 comments:

  1. I just want to say I’m new to blogs and seriously loved this web site. Probably I’m likely to bookmark your blog post . You really have great stories. Regards for revealing your web site.

    Linux Training in Chennai Velachery

    ReplyDelete
  2. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    iOS Training in Chennai

    ReplyDelete
  3. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.
    Digital Marketing Company in India
    seo Company in India

    ReplyDelete
  4. Hi,
    I have read your previous posts recently , and let me tell you, your website gives the
    best and the most interesting information. This is just the kind of information that i had
    been looking for.
    Thank you.
    oracle R12 training

    ReplyDelete
  5. I have just read your site, If you are looking for best Oracle financials also giving all software Course Online Training.The oracle Online Training is one of the leading Online Training institute in Hyderabad.
    Oracle fusion financials training

    ReplyDelete


  6. Wonderful blog.. Thanks for sharing informative Post. Its very useful to me.

    Installment loans
    Payday loans
    Title loans

    ReplyDelete

  7. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
    Android App Development Company

    ReplyDelete
  8. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
    iOS App Development Company
    iOS App Development Company

    ReplyDelete