Click here to Skip to main content
15,885,880 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So myh problem is that i want my javascript's if statement to have an input required when entering in the input in my form on HTML but want to include more than 1 id in the id box if that makes sense. this is for my form validation and using this code makes the form not validate and submit straight away

What I have tried:

if(product &&  product1 && product2 && product3 && product4 ==""){
     text="**Input Required**";
	 errormsg.innerHTML = text;
     return false;
    }
Posted
Updated 14-Apr-21 5:52am
v3

1 solution

Comparison from your code is wrong. Value needed for each variables. Below one is sample valid example.
JavaScript
if(UserID == "" && Password =="")/*If both UserID and Password are empty*/
{
....
}

Sometime you need to other logical character. For example, above code is wrong in one way because fields like UserID and Password are mandatory so you have to use OR condition in validation.
JavaScript
if(UserID == "" || Password =="")/*If UserID or Password are empty*/
{
....
}

JavaScript Comparison and Logical Operators[^]

Here few sample related to display error messages
Simple Javascript Validation without Alerts - Java Infinite[^]
How to display error without alert box using JavaScript ? - GeeksforGeeks[^]

Search Web for more samples.
 
Share this answer
 
v2
Comments
Richard Deeming 13-Apr-21 4:04am    
If both fields are required, you probably want an || instead of an && there. :)
thatraja 13-Apr-21 8:33am    
Yeah, I missed that while answering. His code has == "" only at the end so want to give him sample first. Let me update. Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900