No, I can not use radio. At the help of a veteran programmer, I was able to write the following code and it works.
<script type="text/javascript" src="http://code.jquery.com/jquery- latest.js"></script> <script type="text/javascript" src="http://dev.jquery.com/view/trunk/ plugins/validate/jquery.validate.js"></script> <script type="text/javascript"> <!-- $(document).ready(function() { $("#form1").validate({ rules: { participations: { required: function() { return $('#days').val() == '' ? true : false; } }, days: { required: function() { return $('#participations').val() =='' ? true: false; } }, title: { required: true, minlength:40 } , content: { required: true, minlength:100, maxlength:600 }, evaluation: { required: true, minlength:50, maxlength:300 }, price: { required: true, digits:true }, multi:{ required:true } }, messages: { } }); }); --> </script> There is a minor problem. I wanna change the default position of an alert message which is "This field is required." by default. How to do it? On Oct 12, 2:54 pm, Don Dunbar <salemd1s...@gmail.com> wrote: > I thought of a much simpler solution. If possible could you use radio > buttons for those two choices in your HTML? In a radio button group > selecting one automatically deselects other buttons in the group. Then you > could just use one of the built in validation checks, as you already have > done, to make sure one is checked.DED > > On Mon, Oct 12, 2009 at 1:43 AM, Phper <hi.steven...@gmail.com> wrote: > > > I am not familiar with the syntax of Jquery plugin. > > > On Oct 12, 2:19 pm, Don Dunbar <salemd1s...@gmail.com> wrote: > > > Hi , I double checked on javascript's XOR operator and it only works with > > > bitwise: so you will have to write your own XOR . This isn't hard : > > [code] > > > if (!foo != !bar) [\code] should work for all elements. > > > or this > > > [code]if( ( foo && !bar ) || ( !foo && bar ) )[\code] > > > > For the validator method I haven't actually tried to run this but you can > > > try this:[code] > > > $.validator.addMethod( > > > > "onlyCheckOne", > > > function(value, elements) { > > > // but put whatever you're using to see if either is checked > > > in the next line > > > return this.optional(value) || elements[o]:checked XOR > > > elements[1]:checked; > > > }, > > > "Please check either participations or days, but not both" > > > )[/code] note change XOR to whatever method you decide to use. > > > > Then add the check to your rules. > > > DED > > > > On Mon, Oct 12, 2009 at 12:36 AM, Phper <hi.steven...@gmail.com> wrote: > > > > > <script type="text/javascript" src="http://code.jquery.com/jquery- > > > > latest.js"></script> > > > > <script type="text/javascript" src="http://dev.jquery.com/view/trunk/ > > > > plugins/validate/jquery.validate.js"></script> > > > > > <script type="text/javascript"> > > > > <!-- > > > > $(document).ready(function() { > > > > $("#form1").validate({ > > > > rules: { > > > > title: { > > > > required: true, > > > > minlength:40 > > > > } , > > > > content: { > > > > required: true, > > > > minlength:100, > > > > maxlength:600 > > > > }, > > > > evaluation: { > > > > required: true, > > > > minlength:50, > > > > maxlength:300 > > > > }, > > > > price: { > > > > required: true, > > > > digits:true > > > > > }, > > > > multi:{ > > > > required:true > > > > } > > > > }, > > > > messages: { > > > > > } > > > > }); > > > > }); > > > > > --> > > > > </script> > > > > > As you can see from the code above, "title", "content", "evaluation", > > > > "prices" and "multi" are required. All of them are required. But there > > > > are additional two fields, which are "participations" and "days". Only > > > > one of them is required. Either "participations" or "days" is > > > > required. How to write this code? > > > > > On Oct 12, 10:17 am, Phper <hi.steven...@gmail.com> wrote: > > > > > A good clue. But I still don't know where to write the "if" > > statement. > > > > > It would be good if you can give me an example. > > > > > > On Oct 11, 10:57 pm, Don Dunbar <salemd1s...@gmail.com> wrote: > > > > > > > Hi, if you are using the validation plugin, I believe it has a > > function > > > > > > "addMethod" that allows you to write your own method for the > > > > validation. It > > > > > > requires a name (javascript identifier), a method to check input > > > > against ( > > > > > > in your case A and B would be checked for completion) and a message > > to > > > > > > display when there is an error (i.e. neither A nor B is filled out, > > or > > > > both > > > > > > are). You can get the details for using the "addMethod" function at > > the > > > > > > jQuery Docs page.http://docs.jquery.com/Plugins/Validation > > > > > > The page lists demos and the function you need is toward the bottom > > of > > > > the > > > > > > page. > > > > > > > The logic is fairly straight forward : when the form is being > > filled > > > > out > > > > > > listen for when A and B have focus, remember if either is checked > > or > > > > ignored > > > > > > and check to make sure both are not simultaneously filled out. > > Check > > > > this on > > > > > > submit as well. > > > > > > > Good luck, > > > > > > DED > > > > > > > On Sun, Oct 11, 2009 at 7:42 AM, Phper <hi.steven...@gmail.com> > > wrote: > > > > > > > > How can I write the code in the context of Jquery validate > > function? > > > > > > > > On Oct 11, 12:43 pm, Don Dunbar <salemd1s...@gmail.com> wrote: > > > > > > > > Hi, javascript has an 'xor' operator. It works just like 'or' > > in an > > > > 'if' > > > > > > > > statement except in 'xor' only one side can be true. In a > > normal > > > > 'or' > > > > > > > > statement either side can be true or both can. So you probably > > want > > > > to do > > > > > > > > something like: if ( A XOR B) { } . Then only one can be true > > to > > > > > > > continue > > > > > > > > if both are true the statement returns 'false'.Hope this helps. > > > > > > > > DED > > > > > > > > > On Sat, Oct 10, 2009 at 10:37 PM, Phper < > > hi.steven...@gmail.com> > > > > wrote: > > > > > > > > > > There are two input fields in a form, but only one of them is > > > > > > > > > required, they are not required at the same time. Either A or > > B > > > > is > > > > > > > > > required. ( A is required OR B is required). In other words, > > a > > > > user > > > > > > > > > can input data to field A, or he can input data to filed B, > > but > > > > he can > > > > > > > > > not input data to Both A and B at the same time. > > > > > > > > > > How to implement this constraint in Jquery form validation?