I have a form which has four fields, when the all the fields are empty and submit button is clicked- it should alert "Please select at least one value".
When I enter the value in any of the fields the form should get submitted. I tried the following code but it's not working
function validatefleid() {
var computername = document.myform.computername.value;
var monitorname = document.myform.monitorname.value;
var tvname = document.myform.tvname.value;
var laptopname = document.myform.laptopname.value;
var cellphonename = document.myform.cellphonename.value;
if ((computername == null || computername == "") || (monitorname == null || monitorname == "") || (tvname == null || tvname == "") || (laptopname == null || laptopname == "") || (cellphonename == null || cellphonename == "")) {
alert("Please atleast one value");
return false;
}
}
<form name="myform" onsubmit="return validatefleid()">
computer
<input type="text" class="form-control inpt-bx-txtclr-home" name="computername" id="computerid" placeholder="000">monitor
<input type="text" class="form-control inpt-bx-txtclr-home" name="monitorname" id="monitorid" placeholder="000">tv
<input type="text" class="form-control inpt-bx-txtclr-home" name="tvname" id="tvid" placeholder="000">laptop
<input type="text" class="form-control inpt-bx-txtclr-home" name="laptopname" id="laptopid" placeholder="000">cellphone
<input type="text" class="form-control inpt-bx-txtclr-home" name="cellphonename" id="cellphoneid" placeholder="000">
<button type="submit">Calculate</button>
</form>
document.myform.controlnamewill not work cross-browser, consider replacing bydocument.getElementById('controlid')and use the controls' ids, not names, to refer to them in script.