Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java script not working while validating the fields in php

i am validating 3 fields in html that they are empty or not .

the code is

<input type="file" id="uploadImage" name="image" />file1:
<input type="file" name="QRimage" id="File2" />file2:
<label for="name">Student information:</label>

<input  type="checkbox" name="person" id="person" >Yes

javascript:

   function validate()
       {
     var empt = document.forms["form1"]["image"].value; 
     var empt1 = document.forms["form1"]["QRimage"].value; 
     var empt2 = document.forms["form1"]["person"].value;  

     if (empt == "" && empt1 == "" && empt2 != "checked" )  
       {  
        alert("Please input a Value");  
       return false;  
       }  

Problem: if those 3 fields are empty then its gives the alert message that ("please input a value") . but if checked the check box the allso its giving same message rather than going next page . where i am wrong ?

like image 699
Nayana Avatar asked Jun 27 '26 15:06

Nayana


1 Answers

You want to access the checked property (which is a boolean) of the checkbox, not the value:

var isChecked = document.forms["form1"]["person"].checked;  

if (empt == "" && empt1 == "" && !isChecked) {  
    alert("Please input a Value");  
    return false;  
}  
like image 70
Jivings Avatar answered Jul 01 '26 15:07

Jivings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!