Skip to content

Instantly share code, notes, and snippets.

@mhamzas
Created January 17, 2020 07:11
Show Gist options
  • Save mhamzas/d0e965d725f8647cd4ba7978aee164e3 to your computer and use it in GitHub Desktop.
Save mhamzas/d0e965d725f8647cd4ba7978aee164e3 to your computer and use it in GitHub Desktop.
Custom validation in Lightning web component
/* Method to get input field value */
getinputval() {
let returnvalue; //assigning temp variable
var inputCmp = this.template.querySelector(".inputfield"); //getting element
var value = inputCmp.value; //assigning value to variable
// is input valid text?
if (value === "") {
//adding custom validation
inputCmp.setCustomValidity("Field is Required!");
returnvalue = false;
} else {
//Removing validation error
inputCmp.setCustomValidity(""); // if there was a custom error before, reset it
returnvalue = this.datevalue;
}
inputCmp.reportValidity(); // Tells lightning-input to show the error right away without needing interaction
return returnvalue; // returning inputvalue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment