Jump to content
  • 0

How To Prevent Future Date Selection / Entry


MetricsCC
 Share

Question

I have a date field that represents the date that a member of the database received their certification.  A future date would be invalid.  I would like to prevent the entry (either typing the date or selecting from the calendar) of a date in the future from "TODAY'S DATE." 

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

The ID in here does not seem to be accurate. If it is a submission page and mcc_bootcamp_date is the field name the ID should be

InsertRecordmcc_bootcamp_date

 

Refer to http://forums.caspio.com/index.php/topic/4377-js-guide-caspio-form-elements/

 

You will not see the error unless you have enabled developer tool on your browser.

Link to comment
Share on other sites

  • 0

You can add an HTML Block right after the date field and use this script:

<script>
function pastDateValidation()
{
    var d=document.getElementById("InsertRecordDate").value;
        if(new Date(d) > new Date())
    {
        alert("Select a date which is before now");
        document.getElementById("InsertRecordDate").value="";
    }

}
document.getElementById('InsertRecordDate').onchange = pastDateValidation;

</script>

InsertRecordDate needs to be replaced with the actual ID field

 

Also make sure to click on source then paste the code

Link to comment
Share on other sites

  • 0

I am not receiving an error.  It just simply accepts the future date without preventing me from entering the record.  This is a submission form, not an update form in case that makes a difference. 

 

<script>
function pastDateValidation()
{
    var d=document.getElementById("mcc_bootcamp_date").value;
        if(new Date(d) > new Date())
    {
        alert("Select a date which is prior to today.");
        document.getElementById("mcc_bootcamp_date").value="";
    }
 
}
document.getElementById('mcc_bootcamp_date').onchange = pastDateValidation;
 
</script>
Link to comment
Share on other sites

  • 0

For anyone using UK dates: the above did not work for me. 

This did though:

<script type="text/javascript">
document.addEventListener('DataPageReady', assignAdditionalListeners);
let dateInputField= document.getElementById("InsertRecordDate");

function assignAdditionalListeners() {
dateInputField.addEventListener('change', alertPastDate);
document.removeEventListener('DataPageReady', assignAdditionalListeners);
}

function alertPastDate(event) {

dateChosen = dateInputField.value;
split = dateChosen.split('/');
var dateChosenConverted = [split[1], split[0], split[2]].join('/');
console.log(dateChosenConverted);

if ( new Date( dateChosenConverted ) > new Date() ) {
alert( 'No Future Dates Accepted' );
dateInputField.value = "";
}
}

</script>

 

Link to comment
Share on other sites

  • 0

Hi - these might be useful

 

The key difference tho is the script that is going to be placed on the footer. You can use the following script instead:

<script language="javascript">
    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0');
    var yyyy = today.getFullYear();

    today = yyyy + '-' + mm + '-' + dd;
    $('#date_picker').attr('max',today);
</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
Answer this question...

×   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...
 Share

×
×
  • Create New...