Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have created a web page that contains 2 text boxes that allow to enter two dates and javascript is used to validate the two text boxes are that empty. The out put is not coming as it wishes!

If the two text boxes are empty the javascript alert want work properly.

I post the source code at the below!

HTML
  <pre>!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function Validate()
{ var CheckInDate = document.getElementById(CheckIn).value;
  var CheckOutDate = document.getElementbyId(CheckOut).value;
  if var CheckInDate = "" || var CheckOutDate = "" {
    alert ("One or more fields empty"); } }
    </script>
    </head>
    <body>
    <form id="form1" name="form1" method="post" action="">
    <label>CheckIn Date <input type="text" name="CheckIn" id="CheckIn" /> </label>
    <p>
    <label>CheckOut Date <input type="text" name="CheckOut" id="CheckOut" />
    </label> </p> <p> <label>
    <input type="submit" name="Submit" id="Submit" value="Submit" onclick="Validate()" />
    </label>
    </p>
    </form>
    </body>
    </html>
Posted
Updated 29-Dec-11 20:46pm
v2

Your script is wrong, try this
JavaScript
function Validate()
{ var CheckInDate = document.getElementById('CheckIn').value;
  var CheckOutDate = document.getElementbyId('CheckOut').value;
  if(CheckInDate == "" || CheckOutDate == "") 
  {
    alert("One or more fields empty"); 
  } 
}
 
Share this answer
 
v2
Comments
koolprasad2003 30-Dec-11 3:01am    
Hey Raja, this is javascript. have you assign blank value "" to CheckInDate.
Please change assignment operator to comparison operator.
thatraja 30-Dec-11 3:06am    
Oops my bad, Sometimes it happens because worked in C#, VB, Classic ASP, js, etc., things sometimes confusing.
Anyway thanks.
koolprasad2003 30-Dec-11 3:50am    
Same thing with me too. i also work with C#, VB many times.
Chiranthaka Sampath 30-Dec-11 4:03am    
The posted coding is not working!
check following Javascript function
C#
function Validate()
{ 
  var CheckInDate = document.getElementById('CheckIn').value;
  var CheckOutDate = document.getElementbyId('CheckOut').value;
  if(CheckInDate == "" || CheckOutDate == "")
  {
    alert("One or more fields empty");
    return false;
  }
}
 
Share this answer
 
v2
Comments
Chiranthaka Sampath 30-Dec-11 4:04am    
Same result not working!
modify this in your js function :
C#
function Validate()
{ var CheckInDate = document.getElementById(CheckIn).value;
  var CheckOutDate = document.getElementbyId(CheckOut).value;
  if var CheckInDate = "" || var CheckOutDate = "" {
    alert ("One or more fields empty");

return false;}

return true;}

and in input tag in design :
HTML
<input type="submit" name="Submit" id="Submit" value="Submit" onclick="return Validate()" />


Hope this will help you.
Don't forget to mark as answer if it helps. :)
 
Share this answer
 
Comments
Chiranthaka Sampath 30-Dec-11 4:04am    
Same result not working!
use below function

C#
function Validate()
{ var CheckInDate = document.getElementById(CheckIn).value;
  var CheckOutDate = document.getElementbyId(CheckOut).value;
  if (CheckInDate = "" || CheckOutDate = "") {
    alert ("One or more fields empty"); return false; }return true; }


and change in button

<input type="submit" name="Submit" id="Submit" value="Submit" onclick="return Validate()" />
 
Share this answer
 
Comments
Chiranthaka Sampath 30-Dec-11 4:02am    
I have use the above coding but gave the same result!
Hi, 

Change This also

<label>CheckIn Date</label> <input type="text" name="CheckIn" id="CheckIn" /> 
    <p>
    <label>CheckOut Date</label> <input type="text" name="CheckOut" id="CheckOut" />
     </p> <p> 
    <input type="submit" name="Submit" id="Submit" value="Submit" onclick="Validate()" />

Now Try.</p>
 
Share this answer
 

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