Form validation - atleast 1 field is mandatory out of 3 fields with bootstrap

I have a form that I am trying to validate that has multiple fields
in that I kept 3 fields are mandatory, in that 3 fields user can fill
any 1 field.

What I’d like to do is add an additional validation rule that says: the form is not valid if 3 fields siteName, siteID and AdvSearchZipcode are blank.
In other words, I’d like to make it such that at least one field siteName, siteID or AdvSearchZipcode is required.

I have below jquery code for validation but didn’t understand how to integrate with bootstrap framework and to show error in this tag “<div class="help-block"></div>”, if field is blank.

your early reply would appreciated…

Thanks is advance.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Megapath - Ticket Search Options</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="../assets/css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="../assets/css/dataTables.colVis.css">
<link href="../assets/css/bootstrap.css" rel="stylesheet">
<link href="../assets/css/ump-style.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="../assets/css/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="../assets/css/jquery-ui.theme.css">
<link href="../assets/css/bootstrap-responsive.css" rel="stylesheet">

<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
      <script src="../assets/js/html5shiv.js"></script>
    <![endif]-->

</head>
<body>
<form id="ticketSearch" class="form-horizontal error" novalidate>
<div class="control-group">
                              <label for="siteName" class="control-label"><span class="mr3 redText">*</span>Site Name:</label>
                              <div class="controls">
                                <input type="text" data-validation-required-message="Please Enter Site Name" required="" class="input-large" placeholder="Site Name" name="siteName" id="siteName">
                              <div class="help-block"></div>
                              </div>
                            </div>
                            <div class="control-group">
                              <label for="siteID" class="control-label"><span class="mr3 redText">*</span>Site ID:</label>
                              <div class="controls">
                                <input type="text" data-validation-required-message="Please Enter Site ID" required="" class="input-large" placeholder="Site ID" name="siteID" id="siteID">
                              <div class="help-block"></div>
                              </div>
                            </div>
                            <div class="control-group">
                              <label for="serviceAddress" class="control-label">Service Address:</label>
                              <div class="controls">
                                <input type="text" class="input-large" placeholder="Service Address" name="serviceAddress" id="serviceAddress">
                              </div>
                            </div>
                            <div class="control-group">
                              <label for="AdvSearchCity" class="control-label">City:</label>
                              <div class="controls">
                                <input type="text" class="input-large" placeholder="City" name="AdvSearchCity" id="AdvSearchCity">
                              </div>
                            </div>
                            <div class="control-group">
                              <label for="AdvSearchState" class="control-label">State:</label>
                              <div class="controls">
                                <input type="text" class="input-large" placeholder="State" name="AdvSearchState" id="AdvSearchState">
                              </div>
                            </div>
                            <div class="control-group">
                                  
                              <label for="AdvSearchZipcode" class="control-label"><span class="mr3 redText">*</span>Zip Code:</label>
                              <div class="controls">
                                <input type="text" data-validation-required-message="Please Enter Zip Code" required="" class="input-large" placeholder="Zip Code" name="AdvSearchZipcode" id="AdvSearchZipcode">
                                <div class="help-block"></div>
                              </div>
                            </div>
                            <div class="control-group">
                              <label for="AdvSearchInstallphone" class="control-label">Install Phone:</label>
                              <div class="controls">
                                <input type="text" class="input-large letterSpace7 phoneNumber" placeholder="000-000-0000" name="AdvSearchInstallphone" id="AdvSearchInstallphone">
                              </div>
                            </div>
</form>

<script src="../assets/js/jquery.js" type="text/javascript"></script>
<script src="../assets/js/jqBootstrapValidation.js"></script>
<script type="text/javascript" src="../assets/js/datatable/jquery.dataTables.js"></script>
<script type="text/javascript" src="../assets/js/jquery_ui/jquery-ui.js"></script>
<script src="../assets/js/placeholder.js"></script>
<script src="../assets/js/jquery.maskedinput.js"></script>

$(document).ready(function () {
/* Form Validation */    
    $(function () { $("input,select,textarea").not("[type=submit]").jqBootstrapValidation(); } );

$("#ticketSearch").submit(function() {
    var siteName = $('#siteName');
    var siteID = $('#siteID');
    var AdvSearchZipcode = $('#AdvSearchZipcode');

    if(siteName.val() == '' && siteID.val() == '' && AdvSearchZipcode.val() =='') {
        alert('Fill out any 1 fields');
    }
    else if(siteName.val() == '') {
        alert('siteName, please...');
    }
    else if(siteID.val() == '') {
        alert('siteID, please...');      
    }    
    else if(AdvSearchZipcode.val() == '') {
        alert('AdvSearchZipcode, please...');      
    }
    
    else {
        alert('Yay!');
    }   
});

})

</body>
</html>

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

Before I dive into this, does it work if you wrap the code at the bottom with script tags? So this:

<script>
    $(document).ready(function () {
    /* Form Validation */    
        $(function () { $("input,select,textarea").not("[type=submit]").jqBootstrapValidation(); } );

    $("#ticketSearch").submit(function() {
        var siteName = $('#siteName');
        var siteID = $('#siteID');
        var AdvSearchZipcode = $('#AdvSearchZipcode');

        if(siteName.val() == '' && siteID.val() == '' && AdvSearchZipcode.val() =='') {
            alert('Fill out any 1 fields');
        }
        else if(siteName.val() == '') {
            alert('siteName, please...');
        }
        else if(siteID.val() == '') {
            alert('siteID, please...');      
        }    
        else if(AdvSearchZipcode.val() == '') {
            alert('AdvSearchZipcode, please...');      
        }
        
        else {
            alert('Yay!');
        }   
    });

    })
</script>

Thanks for your reply,
I have checked this out in my Page its not working properly, if you have any better solution to handle this please suggest me…

Regards

1/ I don’t understand why after you check that at least one field is populated, you have to check each one individually.
2. If whilte spaces are not valid values, use the function ‘trim’, for example:

  siteName.val().trim == ''

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.