Jump to content

Validate Date and Time


Recommended Posts

I am trying to make sure the time in entered is less than the time out entered.  I was determined to figure this out on my own but just can get go any further without help. Here is my code. Since Defined_Date_Out is required, I thought replacing the users erroneous entry with null would prevent the submission from being initiated.  Thanks for any advise ?

 

<div id="datefix">
<script>
function DateCheck()
 {

var dateout = document.getElementById('Defined_Time_Out').value;
var datein = document.getElementById('Defined_Time_In').value;
 
if (dateout <=  datein)

alert("Please enter a date and time OUT after date and time IN ");

document.getElementById("InsertRecordDefined_Time_Out").value = null;
}
 
document.getElementById('InsertRecordDefined_Time_Out').onChange = DateCheck;

</script>
 </div>
 

Link to comment
Share on other sites

Hello Accountability,

I would suggest using following code if you prefer to restrict form submission if the condition "dateout <=  datein" is met: 

<div id="datefix">
<script>
function DateCheck()
 {

var dateout = document.getElementById('InsertRecordDefined_Time_Out').value;
var datein = document.getElementById('InsertRecordDefined_Time_In').value;
 
if (dateout <=  datein)

alert("Please enter a date and time OUT after date and time IN ");

document.getElementById("InsertRecordDefined_Time_Out").value = null;
   
return false;
}
 
document.getElementById('InsertRecordDefined_Time_Out').onChange = DateCheck;

</script>
 </div>

Hope this helps.

 

Link to comment
Share on other sites

Also unsuccessfully tried this: 

<div id="datefix">
<script>
function DateCheck()
 {

var dateout = document.getElementById('InsertRecordDefined_Time_Out').value;
var datein = document.getElementById('InsertRecordDefined_Time_In').value;
var zero = "00/00/0000"

if (dateout <=  datein)

alert("Please enter a date and time OUT after date and time IN ");

document.getElementById("InsertRecordDefined_Time_Out").value = "zero";
   
return false;
}
 
document.getElementById('InsertRecordDefined_Time_Out').onChange = DateCheck;

</script>
 </div>

Any  suggestions  would be greatly appreciated.   Thanks in Advance. !

Link to comment
Share on other sites

Hi Accountability,

Try the following code:

<div id="datefix">
<script>
function DateCheck()
 {
   
var dateout = document.getElementById('InsertRecordDefined_Time_Out').value;
var datein = document.getElementById('InsertRecordDefined_Time_In').value;

if (dateout <=  datein)

alert("Please enter a date and time OUT after date and time IN ");

document.getElementById("InsertRecordDefined_Time_Out").value = null;
   
return false;
}
 
document.getElementById('InsertRecordDefined_Time_Out').onchange = DateCheck;

</script>
 </div>

I have corrected some minor syntax errors.

Hope this helps. 

Link to comment
Share on other sites

15 hours ago, Accountability said:

Hello Vitalikssssss,

Thanks again for responding. Unfortunately, this does not work.  It is really strange because it is such a simple script and with the limited knowledge I have in regards to Javascript, it appears to be flawless. I am really scratching my head on this one. Thanks again.

Hi Accountability,

Have you put this part in a Header of the Datapage?

<div id="datefix">

Can you send me a screenshot from your browser console?  Perhaps we`ll be able to trace an error from there.

 

Regards,

vitalikssssss

Link to comment
Share on other sites

Hi there,

I just tested this code and it works on form submit. Just remember that this Caspio Form should be the only one deployed in your webpage. 

<script>
document.getElementById("caspioform").onsubmit = function(){
var dateOutString = document.getElementById('InsertRecordDefined_Time_Out');
var dateInString = document.getElementById('InsertRecordDefined_Time_In');
var dateOut = new Date(dateOutString.value).getTime();
var dateIn = new Date(dateInString.value).getTime();

if (dateOut <=  dateIn){
alert("Please enter a date and time OUT after date and time IN ");
dateOutString.value = '';
document.getElementById("cbParamVirtual6").value = "";
document.getElementById("cbParamVirtual7").selectedIndex = "";
document.getElementById("cbParamVirtual8").selectedIndex = "";
document.getElementById("cbParamVirtual10").selectedIndex = "";
return false;
}
};
</script>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...