3

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 ?

3
  • where you have applied the validate() ? provide complete form (HTML). Commented Feb 10, 2015 at 11:12
  • The value of empt2 will never be 'checked', you've to compare ][person].checked (which will be a boolean) instead of value. What has PHP to do with this question? Commented Feb 10, 2015 at 11:12
  • oh ho thank you @teemu you are true :) Commented Feb 10, 2015 at 11:16

1 Answer 1

3

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;  
}  
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry Jivings it was not working with "!isChecked" . i used empt2 == "" then its working . anyway thanks for helping

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.