admin管理员组

文章数量:1420078

I'm trying to add validation before form submit. There's an alternative input field, let's call this "Optional" and there's checkboxes "Checkbox1", "Checkbox2", "Checkbox3". When user choose one from this checkboxes, then he must fill input "Optional", if it is empty then he can't submit form and get's some pop-up alert with info to fill this.

I have something like this:

<script>
function validateForm() {
    var x = document.forms["form"]["nrumowy"].value;
    if (x == null || x == "") {
        alert("Numer umowy musi zostać uzupełniony.");
        return false;
    }
}
</script>

But this input field "nrumowy" must be filled in all situations, so everything okay, and now I want add something similiar but only when one of "Checkbox1", "Checkbox2", "Checkbox3" are checked.

I'm trying to add validation before form submit. There's an alternative input field, let's call this "Optional" and there's checkboxes "Checkbox1", "Checkbox2", "Checkbox3". When user choose one from this checkboxes, then he must fill input "Optional", if it is empty then he can't submit form and get's some pop-up alert with info to fill this.

I have something like this:

<script>
function validateForm() {
    var x = document.forms["form"]["nrumowy"].value;
    if (x == null || x == "") {
        alert("Numer umowy musi zostać uzupełniony.");
        return false;
    }
}
</script>

But this input field "nrumowy" must be filled in all situations, so everything okay, and now I want add something similiar but only when one of "Checkbox1", "Checkbox2", "Checkbox3" are checked.

Share Improve this question asked Apr 15, 2015 at 10:41 user3180931user3180931 872 silver badges11 bronze badges 3
  • 1 please show the html code to sort out problem. – Noman Commented Apr 15, 2015 at 10:46
  • Then why don't you add conditions for what you want? – Imran Commented Apr 15, 2015 at 10:46
  • @Imran I'm newbie at JS, i don't know how to add conditions to make this works. – user3180931 Commented Apr 15, 2015 at 11:00
Add a ment  | 

4 Answers 4

Reset to default 3

Following code will work instantly while filling the form.

jquery code

<script>
function validateForm() {
    var x = document.forms["form"]["nrumowy"].value;
    if (x == null || x == "") {
        alert("Numer umowy musi zostać uzupełniony.");
        return false;
    }
    var op = document.forms["form"]["optional"].value;
    if ($("input[type=checkbox]:checked").length === 0 && op==""){
        alert("you must fill Optional field");
        return false;
    }
}
</script>

If you want to add something similiar but only when one of "Checkbox1", "Checkbox2", "Checkbox3" are checked. Then see if following code helps you.

function validateForm() {
  
  if(document.getElementById("Checkbox1").checked  ||
     document.getElementById("Checkbox2").checked  ||
     document.getElementById("Checkbox3").checked  ||
     {
    
    var x = document.forms["form"]["nrumowy"].value;
    if (x == null || x == "") {
        alert("Numer umowy musi zostać uzupełniony.");
        return false;
    }
     
     }
}

You should simply get the values of these checkboxes before that, and if one of them is not checked, exit the function.

function validateForm() {
    var frm = document.forms["form"];
    if (!frm["Checkbox1"].checked && !frm["Checkbox2"].checked && !frm["Checkbox3"].checked) return;

    var x = frm["nrumowy"].value;
    if (x == null || x == "") {
        alert("Numer umowy musi zostać uzupełniony.");
        return false;
    }
}

Ok, thanks for your answers, I got now this:

<script>
function validateForm() {
    var frm = document.forms["form"];
    if (!frm["check_list1"].checked && !frm["check_list2"].checked && !frm["check_list3"].checked) return;

    var x = frm["tematyka"].value;
    if (x == null || x == "") {
        alert("Tematyka artykułu musi zostać uzupełniona.");
        return false;
    }


}
</script>
<script>
function validateForm2() {
var x2 = document.forms["form"]["nrumowy"].value;
    if (x2 == null || x2 == "") {
        alert("Numer umowy musi zostać uzupełniony.");
        return false;
    }
}
</script>

HTML:

<form name="form" method="POST" action="preview.php" onsubmit="return (validateForm() & validateForm2());">

And alerts works, but after user click OK then he goes to preview.php page. When there's only one onsubmit function then this works great, but when I try to bine this two functions then there is a some error.

本文标签: javascriptForm validation empty field when checkbox is checkedStack Overflow