function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
@ M  Coder@ M Coder 

lwc js :SIMPLE fix needed - if future date is selected need to throw error message ...

Hi Team , 

Can you fix this if  User  select a future date say tmoro date it should throw an error in UI.  user should only select todays date or previous date

somehow my snippet is working wrong

<lightning-input  type="date" name="Date entry" class="dateCls" value={Dateentry} data-name="Dateentry" onchange={handledate} > </lightning-input>
    
 handledate(event){
        console.log('event==called=');
        if(event.target.name === "Dateentry") {
          let psdate = this.template.querySelector(".dateCls"); 
            var today  = new Date();
            console.log('today==+++=='+today);
            if(event.target.value > today)
            {
               psdate.setCustomValidity("Please Select old Date.");
            }
            else{
            this.SelectedDate = event.target.value;
            console.log('this.SelectedDate =+==='+this.SelectedDate );
            }
        }
    }        
 
AshwiniAshwini (Salesforce Developers) 
Hi @M Coder,
You can try something like below to set custom error message:
<lightning-input type="date" name="Date entry" class="dateCls" value={Dateentry} data-name="Dateentry" onchange={handledate}></lightning-input>
<div class="error-message">{customErrorMessage}</div>
 
import { LightningElement, track } from 'lwc';

export default class MyComponent extends LightningElement {
    @track Dateentry;
    @track customErrorMessage = ''; 
    handledate(event) {
        if (event.target.name === 'Dateentry') 
       {
           var selectedDate = new Date(event.target.value);
            var today = new Date();
            console.log('today==+++==' + today);
            if (selectedDate > today) 
           {
              this.customErrorMessage = 'Please Select an old Date.';
            } else 
            {
                this.SelectedDate = event.target.value;
                console.log('this.SelectedDate =+===' + this.SelectedDate);
                this.customErrorMessage = ''; 
            }
        }
    }
}
Related:https://techdicer.com/custom-validation-in-lightning-web-component/
https://www.infallibletechie.com/2022/01/how-to-show-error-message-in-lightning.html 
https://medium.com/salesforce-champion/lightning-web-component-custom-validations-4f4d87e119b9

If this information helps, please mark the answer as best. Thank you