0

I have a form with 2 number inputs and I am trying to validate it using the jQuery Validation plugin. I want to allow the form to be submitted only if the sum of the values is less than or equal to 10. For example, I want to allow 3 and 6, but not 7 and 8. How can I do this?

I was able to come up with something that works but I think it is inelegant and fragile to arbitrarily apply the rule to one input and use an attribute on that input to refer to the other input. I looked in the documentation for the plugin and I don't see any way of defining a validator that is meant to be used on more than one input. Can anyone think of a simpler way?

Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
  • This is so specific that there's no point in using the jQuery validator at all. Just call your own custom function that looks ad these fields directly. – Diodeus - James MacFarlane Oct 10 '12 at 21:00
  • I think it would be cumbersome to use the validation plugin for all of the validation except this thing. Other people working on my project may have trouble figuring out what I am doing. – Elias Zamaria Oct 10 '12 at 21:04
  • The validator may be the wrong tool for the job. It seems more work getting the tool to do what you want vs. writing a few lines of code and providing comments and documentation explaining why. – Diodeus - James MacFarlane Oct 10 '12 at 21:07
  • The plugin does some useful things like showing error messages and focusing invalid inputs. If I use my own code just to validate those 2 fields, I am not sure if I can get all the details right and make it act the same as the others. I am sort of considering validating them without the plugin but I am uncertain, hence the reason that I am asking for suggestions before deciding to go ahead and do it. – Elias Zamaria Oct 10 '12 at 21:28
  • I'd try using the same HTML/CSS the validator is dropping in. Maybe this will help: http://stackoverflow.com/questions/4225121/jquery-validate-sum-of-multiple-input-values – Diodeus - James MacFarlane Oct 10 '12 at 21:33
  • Thanks for the link. I didn't notice that question. Feel free to mark this one as a duplicate. It is not exactly what I am trying to do but it is close enough that I can probably figure it out without too much trouble. – Elias Zamaria Oct 10 '12 at 21:36

1 Answers1

0

As far as the jquery validation plugin. No. A simpler way. Yes write your own in plain javascript.

function mySubmitfuntcion()
{
var myvar1 = document.GetElementById("1").value;
var myvar2 = document.GetElementById("2").value;

if(myvar1 + myvar2 <= 10) { 
return true;
}

else {
alert("Please enter valid values");

return false;
} // end if
} // end function

returning false stops the mySubmitfunction() from completing.

In my experience writing custom validation scripts works better because you have more control. Remember to add the validation function to a button or the forms onsubmit event.

user1015711
  • 134
  • 1
  • 17
  • I want to use the jQuery Validation plugin because it has features such as placing error messages and focusing invalid fields. See my comment above. – Elias Zamaria Oct 10 '12 at 21:54