0

I am using

[Jquery validation plugin][1] 


  [1]: https://jqueryvalidation.org, 

How can i validate if at least one of three options was filled ?

Creating a method via .addMethod ? i am trying to adapt from this fiddle but i have text boxes instead of select boxes:

 [validate select box][1]


  [1]: http://jsfiddle.net/8x1yj0pr/

I did not get any message, here is what i try until now:

$.validator.addMethod("validateOptions", function(value, element) {
        return this.optional( element ) ||
        ($('#firstname').val() != '' ||
        $('#username').val() != '' ||
            $('#email').val() != '' ||);
}, "Please provide at least one ");

$(document).ready(function() {
        var validator = $("#VerifyForm").validate({
            rules: {
                formFields: {
                   validateOptions: true,
                }
                firstname : {
                    required: true,
                    minlength: 2
                },
                lastname : {
                    required: true,
                    minlength: 7
                },
                email : {
                    required: true,
                    minlength: 2
                },
            },
1
  • Can you post your html code and full javascript Commented Jun 17, 2016 at 13:21

1 Answer 1

1

No need to use a plugin just for that. Try this:

jsFiddle Demo

$('#submit').click(function(evt){ //<===============================
  var fn = $('#fn').val();
  var un = $('#un').val();
  var em = $('#em').val();
  if (fn !='' || un!='' || em!=''){
    alert('good to go');
  }else{
    evt.preventDefault(); //<===============================
    $('#fn').focus();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="text" id="fn" /><br>
<input type="text" id="un" /><br>
<input type="text" id="em" /><br>
<input type="button" id="submit" value="Submit" />

Sign up to request clarification or add additional context in comments.

8 Comments

This work´s right, but how can i prevent the form from being submitted ? as i am using CakePHP 2.5
@ÂngeloRigo See modification to my answer. event.preventDefault() should do the trick.
No, it suppresses the default action of the <form>, which is to submit to either: (1) the page specified in the action=xxxxx.php tag attribute, or, if that is missing, (2) to the same page the form tag is on (which causes a page refresh)
Angelo, I am busy right now with something else. If you ask this as a separate question (reference this post in your question), you will get your question read instantly by dozens of people who will try to help. That will be much faster than waiting for me. Best wishes for your project.
No, just at stackoverflow.com - like you posted this one. Then include the URL to this post in your new question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.