开发者

ASP.NET: Multiple Validation

开发者 https://www.devze.com 2023-03-17 17:56 出处:网络
I\'m looking for a way make a custom validator that compares the sum of four field set with the sum of a different four field set, before inserting / editing the database but so far my effort haven\'t

I'm looking for a way make a custom validator that compares the sum of four field set with the sum of a different four field set, before inserting / editing the database but so far my effort haven't been all that successful.

An example on how it should work is:

f11: 0        f21: 50
f12: 0        f22: 50
f13: 200      f23: 50
f14: 0     开发者_开发技巧   f24: 50
---> Valid---> insert/update    

f11: 150      f21: 10
f12: 0        f22: 150
f13: 0        f23: 5
f14: 19       f24: 0 
---> Invalid---> return alert('F1* does not match with F2*') 

I've tried making my own but I'm pretty much at a standstill


Follow these steps at client-side and server-side both:

  1. Get the values of all first four fields and sum them up
  2. Get the values of all second four fields and sum them up
  3. Compare them, if they're equal, do what you want to do, otherwise show a message

How to do it?

I thing jQuery is a good option for client-side:

 var firstFour = Number($('#first').val()) + Number($('#second').val()) + ...
 var secondFour = Number($('#fifth').val()) + ...
 if (firstFour == secondFour)
 {
    // Do the business here.
 }


Fleshing out @Saeed's answer, here is a good intro to how to hook up client and server side functions to your custom validator

https://web.archive.org/web/20211020145934/https://www.4guysfromrolla.com/articles/073102-1.aspx

The signature of the JS function will look like this:

function ValidateNumbers(sender, args)
  {
    //do Saeed's stuff

    args.IsValid = true; //if you're happy
}

The server-side stuff is outlined on that page too.

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消