1

I have a form with two radio input. For one of these two radio input (type1) I display 2 input text. I would like before submiting the form to check if minimum one of these 2 input text boxes is not empty. But my script doesn't work.

HTML

<input id="type1" value="1" type="radio">
<input id="num1" value="" type="text">
<textarea id="numlist"></textarea>
<input id="type2" value="1" type="radio">

JQUERY

$('#form2').on('submit', function (e) {
    if ($("input:radio[id=type1]").is(":checked")) {
        var comment2 = $('#numliste').val();
        var comment1 = $("#num1").val();
        if ((comment1.length === 0) || (comment2.length === 0)) {
            e.preventDefault();
            alert('You have to write on minimum one input text');
            return false;
        }
    }
});
2
  • 2
    '#numliste' should be '#numlist' Commented Dec 5, 2013 at 15:26
  • Maybe you have an other problem related to your type="radio". You have to add the same name attribute for both type1 and type2 to make them act like "radios" Commented Dec 5, 2013 at 15:39

3 Answers 3

3
$('#form2').on('submit', function (e) {
  var type1_checked = $("#type1").is(":checked");
  if (type1_checked) {
    var comment2 = $('#numlist').val();
    var comment1 = $("#num1").val();
    if ((comment1 == "") && (comment2 == "")) {
        e.preventDefault();
        alert('You have to write on minimum one input text');
        return false;
    }
  }
  return true;
});

EDIT: Also I assume you want the radio buttons to act like options, so you need the property name. See below for an example (I changed the radio values too):

<form id="form2">
  <input name="options" id="type1" value="type1" type="radio">
  <input id="num1" value="" type="text">
  <textarea id="numlist"></textarea>
  <input name="options" id="type2" value="type2" type="radio">
</form>

Which allows to improve the jquery code

$('#form2').on('submit', function (e) {
  var radio_value = $("input[name=options]:checked").val();
  if (!radio_value)
  {
    alert("Choose an option !");
    return false;
  }
  if (radio_value == "type1")
  {
    var comment2 = $('#numlist').val();
    var comment1 = $("#num1").val();
    if ((comment1 == "") && (comment2 == "")) {
        e.preventDefault();
        alert('You have to write on minimum one input text');
        return false;
    }
  }
  return true;
});
Sign up to request clarification or add additional context in comments.

1 Comment

you could simplify the test to if (!comment1 && !comment2)
0

First of all you have a typo in var comment2 = $('#numliste').val(); change it to "numlist" And if you want this work you have to change the || to && in if statement.

Comments

-1

i think this will work

$('#form2').on('submit',function (e) {
    if($("input:radio[id=type1]").is(":checked"))
              {
                var comment2 = $('#numliste').text();
                var comment1 = $("#num1").text();
                if((comment1.length === 0) || (comment2.length === 0))
                {
                    e.preventDefault();
                    alert('You have to write on minimum one input text');
                    return false;
                }
              } });

3 Comments

minimum one of these 2 input text boxes is not empty. With your code, the form won't be submitted if one of the text box is empty. You need an and not an or
No, you still need an and.
Yes, and that's what we want. With your code, if one condition is true (one input is empty, not the other), the form won't be submitted. This is in contradiction with "minimum one is not empty".

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.