Created
January 17, 2020 07:11
-
-
Save mhamzas/d0e965d725f8647cd4ba7978aee164e3 to your computer and use it in GitHub Desktop.
Custom validation in Lightning web component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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