0

This is my form code

<form action="http://localhost/package/insert" accept-charset="utf-8" name="frmAdd" id="frmAdd" method="POST" enctype="multipart/form-data">
            <input type="radio" name="type" id="type_single" value="single" style="float:left">
            <span style="float:left;margin-right:30px;">Single</span>

            <span id="val-type" class="tooltips">Type is required!</span>

            <input type="text" name="single" id="single" />
            <span id="val-single" class="tooltips">Single is required!</span>
            - OR -
            <input type="file" name="single_list">
            <span id="val-single_list" class="tooltips">Single is required!</span>
    </form>

frmAdd function:

$(document).ready(function() {

  $('#frmAdd').ajaxForm({
    target: "#targetForm",
    type: "POST",
    dataType: "json",
    beforeSubmit: function() {
      return checkForm();
    }
  });
});

and this is my checkForm() function

function checkForm() {
    var flag = true;

    if (!$('input:radio:checked').length > 0) {
        $("#val-type").fadeIn('slow');
        flag = false;
    }

    if ($("#type_single").is(':checked')) {
        var single_val = $("#single").val();
        single_list_val = $("#single_list").val();

        if (single_val == '' && $.trim(single_val == '')) {
            $("#val-single").fadeIn('slow');
            flag = false;
        }
    }    

    if (flag == false) {
        setTimeout(function() {
            $('.tooltips').fadeOut('slow');
        }, 3000);
        return false;
    }

    return true;
}

this part:

<input type="text" name="single" id="single" />
<span id="val-single" class="tooltips">Single is required!</span>
- OR -
<input type="file" name="single_list">
<span id="val-single_list" class="tooltips">Single is required!</span>

how to validate at least if one of those input text is filled the form is valid, I don't want to use any jquery plugins to do this.

junior
  • 808
  • 15
  • 25

2 Answers2

1

You can do this:

if($('input[type=text]').filter(function(){ return $(this).val(); }).length != 0){
  // at least one is filled
}
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • Checking value _"attribute"_ instead of value _"property"_ is not cool. – Ram Oct 03 '14 at 05:32
  • Please check this question http://stackoverflow.com/questions/6003819/properties-and-attributes-in-html – Ram Oct 03 '14 at 05:36
0

You can add the following to your validation function:

if (!($('input[name="single"]').val() || $('input[name="single_list"]').val())) {
    flag = false;
}
T J
  • 42,762
  • 13
  • 83
  • 138