0

I have the following three fields:

1st Field:

<input class="emailrequerido" name="emailrecomendado1_<?php echo $patrocinado->idpatroc; ?>" id="emailrecomendado1_<?php echo $patrocinado->idpatroc; ?>" placeholder="[email protected]" type="text" />

2nd Field:

<select name="contactos<?php echo $patrocinado->idpatroc; ?>[]" id="contactos<?php echo $patrocinado->idpatroc; ?>" multiple="3" style="width:49%" class="contactorequerido">

3rd Field:

<select name="grupos<?php echo $patrocinado->idpatroc; ?>[]" id="grupos<?php echo $patrocinado->idpatroc; ?>" multiple="3" style="width:49%" class="gruporequerido">

Submit Field:

<input id="enviarForm" type="submit" class="uk-button uk-float-right" value="<?php echo JText::_('COM_CSTUDOMUS_PATROCINADOR_SEND_RECOM');?>"/>

I need to have always one of the three fields with a value before your allowed to submit.

I have tried the following:

 $("#enviarForm").click(function() {
    if($(".emailrequerido").val() == false){
        $('.contactorequerido').attr('required', true);
        $('.gruporequerido').attr('required', true);
    }else if($(".contactorequerido option:selected").val() == false){
        $('.emailrequerido').attr('required',true);
        $('.gruporequerido').attr('required',true);
    }else if($(".gruporequerido option:selected").val() == false){
        $('.emailrequerido').attr('required',true);
        $('.contactorequerido').attr('required',true);
    }

    if($(".emailrequerido").val() == true){
        $('.contactorequerido').removeAttr('required');
        $('.gruporequerido').removeAttr('required');
    }else if($(".contactorequerido option:selected").val() == true){
        $('.emailrequerido').removeAttr('required');
        $('.gruporequerido').removeAttr('required');
    }else if($(".gruporequerido option:selected").val() == true){
        $('.emailrequerido').removeAttr('required');
        $('.contactorequerido').removeAttr('required');
    }
})
2
  • Please post the client-side code. That is, the code that is sent to the client after being processed by PHP.
    – George
    Commented Mar 24, 2014 at 12:17
  • @oGeez I believe that what your asking is not necessary for what I need to do. Commented Mar 24, 2014 at 12:22

4 Answers 4

1
$(document).ready(function(){
// Disallow submission here initially ...

$(":input").bind("keyup change", function(e) {
var comboVal = $('.emailrequerido').val()+$('.contactorequerido').val()+$('.gruporequerido').val();
    if(comboVal == 'nullnull'){
      // Don't allow!!! May disable or hide submission button
    } else {
      // Allow :D 
    }
});
});
4
  • What if the user only wants to add an email and not select any of the other fields? If only one field has value it should validate Commented Mar 24, 2014 at 12:21
  • :input selector covers all, input and select. Check it: jsfiddle.net/effone/84suD
    – effone
    Commented Mar 24, 2014 at 12:24
  • Do I need to add that under $("#enviarForm").click(function() { ? Commented Mar 24, 2014 at 12:27
  • Don't think so. Disable submission at document ready. and use the function provided to allow submission.
    – effone
    Commented Mar 24, 2014 at 12:30
0

You can do this way:

$("#enviarForm").click(function() {
    if($('[id^="emailrecomendado1_"]').val() == "" || 
       $('[id^="contactos"]').val() == "" || 
       $('[id^="grupos"]').val() == ""){
      alert('one field required.');
    }
});
0

Give them all the same class, for example req. Then do:

$('#enviarForm').submit(function(){
  return $('.req').filter(function(){return $(this).val();}).length > 0;
});
1
  • Sure, it counts how many elements with a class req have a value and returns true if it's more than zero. Commented Mar 24, 2014 at 12:40
0

try this-- might help.. but not sure..

$("#enviarForm").click(function(e) {
    e.preventDefault();
    var error=0;
    if($(".emailrequerido").val() == false){
        $('.contactorequerido').attr('required', true);
        $('.gruporequerido').attr('required', true);
        error++;
    }
    if($(".contactorequerido option:selected").val() == false){
        $('.emailrequerido').attr('required',true);
        $('.gruporequerido').attr('required',true);
         error++;
    }
    if($(".gruporequerido option:selected").val() == false){
        $('.emailrequerido').attr('required',true);
        $('.contactorequerido').attr('required',true);
          error++;
    }

    if($(".emailrequerido").val() == true){
        $('.contactorequerido').removeAttr('required');
        $('.gruporequerido').removeAttr('required');
    }
    if($(".contactorequerido option:selected").val() == true){
        $('.emailrequerido').removeAttr('required');
        $('.gruporequerido').removeAttr('required');
    }
    if($(".gruporequerido option:selected").val() == true){
        $('.emailrequerido').removeAttr('required');
        $('.contactorequerido').removeAttr('required');
    }
    if(error==0){
        ("#formid").submit();
    }
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.