{"id":2432,"date":"2026-10-24T13:00:00","date_gmt":"2026-10-24T07:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/"},"modified":"2026-11-15T11:29:49","modified_gmt":"2026-11-15T05:59:49","slug":"lwc-form-validation","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/","title":{"rendered":"LWC Form Validation and Custom Error Messages Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">LWC Form Validation covers two jobs: stopping bad input before it is sent, and showing a clear message when the server rejects a save. Base components handle most of the first job for you, as long as you ask them to run the check.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/lightning-base-components-overview\/\">Lightning Base Components<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-record-form-edit-view\/\">record forms<\/a>. We will validate an enrolment form for Bright Path Tutors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is LWC Form Validation?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LWC Form Validation is the set of attributes and methods that check user input in a Lightning Web Component. Base components such as <code>lightning-input<\/code> support HTML constraint attributes, and they expose methods to add your own rules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of airport security. The first gate checks the obvious things quickly, and a person handles the unusual case. Built in attributes are the first gate, and your custom code is the second.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why LWC Form Validation Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A user who clicks Save and waits three seconds for a server error has a worse experience than one who sees the problem as they type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">LWC Form Validation also protects your Apex. Most bad data never reaches the server, so your code handles fewer edge cases. Even so, client checks are a convenience, not a security control.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How LWC Form Validation Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">LWC Form Validation runs the built in rules first, then any custom message you set, and shows the result when you ask.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-1-1024x640.png\" alt=\"LWC Form Validation diagram showing built in and custom checks\" class=\"wp-image-2430\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How built in and custom checks combine.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Built in Attributes<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;lightning-input\n    label=\"Student name\"\n    required\n    max-length=\"80\"\n    message-when-value-missing=\"Please enter the student name\"&gt;\n&lt;\/lightning-input&gt;\n\n&lt;lightning-input\n    type=\"number\"\n    label=\"Fee\"\n    required\n    min=\"0\"\n    max=\"50000\"\n    message-when-range-overflow=\"Fee cannot be more than 50,000\"&gt;\n&lt;\/lightning-input&gt;\n\n&lt;lightning-input\n    type=\"email\"\n    label=\"Parent email\"\n    required\n    message-when-type-mismatch=\"Enter a valid email address\"&gt;\n&lt;\/lightning-input&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each rule has its own message attribute, so you never show a generic error.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Attribute<\/th><th>Matching message attribute<\/th><\/tr><\/thead><tbody><tr><td>required<\/td><td>message-when-value-missing<\/td><\/tr><tr><td>min<\/td><td>message-when-range-underflow<\/td><\/tr><tr><td>max<\/td><td>message-when-range-overflow<\/td><\/tr><tr><td>pattern<\/td><td>message-when-pattern-mismatch<\/td><\/tr><tr><td>max-length<\/td><td>message-when-too-long<\/td><\/tr><tr><td>type email or url<\/td><td>message-when-type-mismatch<\/td><\/tr><tr><td>step<\/td><td>message-when-step-mismatch<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Checking Every Field Before Save<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Built in rules do not block a save on their own. You must run the check.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>handleSave() {\n    const inputs = [...this.template.querySelectorAll('lightning-input')];\n    const allValid = inputs.reduce((valid, input) =&gt; {\n        input.reportValidity();\n        return valid &amp;&amp; input.checkValidity();\n    }, true);\n\n    if (!allValid) { return; }\n    this.save();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>checkValidity<\/code> returns true or false silently. <code>reportValidity<\/code> does the same and also shows the message. So call both, as above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Custom Business Rules<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the rule is about your business, not the data type, use <code>setCustomValidity<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>handleFeeChange(event) {\n    const input = event.target;\n    const value = Number(input.value);\n\n    if (value &gt; this.maxAllowed) {\n        input.setCustomValidity('Fee is above your approved limit of ' + this.maxAllowed);\n    } else {\n        input.setCustomValidity('');\n    }\n    input.reportValidity();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Setting an empty string clears the error. Forgetting that line is the single most common bug, because the field stays red forever.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Comparing Two Fields<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>validateDates() {\n    const start = this.template.querySelector('[data-id=\"start\"]');\n    const end = this.template.querySelector('[data-id=\"end\"]');\n\n    if (start.value &amp;&amp; end.value &amp;&amp; end.value &lt; start.value) {\n        end.setCustomValidity('End date must be after the start date');\n    } else {\n        end.setCustomValidity('');\n    }\n    end.reportValidity();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using <code>data-id<\/code> attributes keeps the selector stable when labels change.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Showing Server Errors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Client checks never catch everything. A validation rule or a duplicate rule fires on save, and that message must reach the user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { ShowToastEvent } from 'lightning\/platformShowToastEvent';\nimport { reduceErrors } from 'c\/ldsUtils';\n\nasync save() {\n    try {\n        await createEnrolment({ record: this.record });\n        this.dispatchEvent(new ShowToastEvent({\n            title: 'Saved', variant: 'success'\n        }));\n    } catch (error) {\n        this.dispatchEvent(new ShowToastEvent({\n            title: 'Could not save',\n            message: reduceErrors(error).join(', '),\n            variant: 'error',\n            mode: 'sticky'\n        }));\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Salesforce errors arrive in several shapes, which is why a helper like <code>reduceErrors<\/code> is worth having.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error source<\/th><th>Where the message sits<\/th><\/tr><\/thead><tbody><tr><td>Apex exception<\/td><td>error.body.message<\/td><\/tr><tr><td>Field errors from DML<\/td><td>error.body.output.fieldErrors<\/td><\/tr><tr><td>Page level errors<\/td><td>error.body.output.errors<\/td><\/tr><tr><td>Network failure<\/td><td>error.body is a plain string<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Client vs Server Validation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This table shows why you need both.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Client side<\/th><th>Server side<\/th><\/tr><\/thead><tbody><tr><td>When it runs<\/td><td>As the user types<\/td><td>On save<\/td><\/tr><tr><td>Speed<\/td><td>Instant<\/td><td>After a round trip<\/td><\/tr><tr><td>Bypassable<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Enforces data integrity<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Best for<\/td><td>User guidance<\/td><td>Data protection<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-2-1024x640.png\" alt=\"LWC Form Validation comparison of client and server checks\" class=\"wp-image-2431\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Client validation compared with server validation.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, client checks make the form pleasant. Server rules keep the data correct.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real LWC Form Validation Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Bright Path enrols a student. The form must require a name and email, cap the fee by the user&#8217;s limit, and show any Apex error clearly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Template<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;template&gt;\n    &lt;lightning-card title=\"New enrolment\"&gt;\n        &lt;div class=\"slds-var-m-around_medium\"&gt;\n            &lt;lightning-input\n                data-id=\"name\"\n                label=\"Student name\"\n                required\n                message-when-value-missing=\"Student name is required\"&gt;\n            &lt;\/lightning-input&gt;\n\n            &lt;lightning-input\n                data-id=\"email\"\n                type=\"email\"\n                label=\"Parent email\"\n                required\n                message-when-type-mismatch=\"Enter a valid email\"&gt;\n            &lt;\/lightning-input&gt;\n\n            &lt;lightning-input\n                data-id=\"fee\"\n                type=\"number\"\n                label=\"Fee\"\n                required\n                min=\"0\"\n                onchange={handleFeeChange}&gt;\n            &lt;\/lightning-input&gt;\n\n            &lt;lightning-button\n                label=\"Enrol\"\n                variant=\"brand\"\n                onclick={handleSave}&gt;\n            &lt;\/lightning-button&gt;\n        &lt;\/div&gt;\n    &lt;\/lightning-card&gt;\n&lt;\/template&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: The Logic<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import { LightningElement, wire } from 'lwc';\nimport getLimit from '@salesforce\/apex\/DiscountGuard.maxFee';\n\nexport default class EnrolForm extends LightningElement {\n    maxAllowed = 0;\n\n    @wire(getLimit) wiredLimit({ data }) {\n        if (data) { this.maxAllowed = data; }\n    }\n\n    handleFeeChange(event) {\n        const input = event.target;\n        const over = Number(input.value) &gt; this.maxAllowed;\n        input.setCustomValidity(over\n            ? 'Fee is above your limit of ' + this.maxAllowed\n            : '');\n        input.reportValidity();\n    }\n\n    handleSave() {\n        const inputs = [...this.template.querySelectorAll('lightning-input')];\n        const valid = inputs.reduce(\n            (ok, i) =&gt; { i.reportValidity(); return ok &amp;&amp; i.checkValidity(); }, true);\n        if (!valid) { return; }\n        this.save();\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: What the User Sees<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">These are the messages the form produces.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Situation<\/th><th>Message shown<\/th><\/tr><\/thead><tbody><tr><td>Name left blank<\/td><td>Student name is required<\/td><\/tr><tr><td>Email missing the at sign<\/td><td>Enter a valid email<\/td><\/tr><tr><td>Fee above the limit<\/td><td>Fee is above your limit of 10000<\/td><\/tr><tr><td>Validation rule fails on save<\/td><td>The rule&#8217;s own message in a toast<\/td><\/tr><tr><td>Network drops<\/td><td>Could not save, with the raw error<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with LWC Form Validation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Forgetting to clear setCustomValidity with an empty string.<\/li>\n\n<li>Calling checkValidity without reportValidity, so nothing is shown.<\/li>\n\n<li>Relying on client checks for security, which an API call bypasses.<\/li>\n\n<li>Querying inputs by label, which breaks when the label changes.<\/li>\n\n<li>Swallowing the catch block, so the user sees nothing after a failed save.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for LWC Form Validation<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use built in attributes first, since they handle accessibility.<\/li>\n\n<li>Give every rule its own message attribute.<\/li>\n\n<li>Clear custom validity on every change, not only on failure.<\/li>\n\n<li>Use data-id attributes for stable selectors.<\/li>\n\n<li>Always enforce the real rule in Apex or a validation rule as well.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Build a form with a required text field and a number field capped at 100. Show a custom message when the number is too high, and clear it when corrected. Hint: setCustomValidity with an empty string clears it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked LWC Form Validation Interview Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These LWC Form Validation questions come up in component interviews.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic LWC Form Validation Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is LWC Form Validation?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">LWC Form Validation is how a component checks user input, using built in attributes on base components plus your own custom rules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: Which attribute makes a field mandatory?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In LWC Form Validation, the required attribute on lightning-input does it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What does checkValidity do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It returns true or false for the field, but it shows nothing to the user.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What does reportValidity do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It checks the field and also displays the error message on screen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: How do you set your own error message?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call setCustomValidity on the input with your text, and then call reportValidity to show it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: How do you clear a custom error?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call setCustomValidity with an empty string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: Which attribute limits text length?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The max-length attribute, with message-when-too-long for the message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Do client side checks protect data?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. They guide the user, while Apex and validation rules protect the data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Intermediate LWC Form Validation Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: How do you validate every field at once?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Query all the inputs, call reportValidity on each and combine the checkValidity results.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Why use data-id instead of a class selector?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because it is a stable hook that does not change when styling or labels change.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: How do you compare two fields, such as dates?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read both values in a handler, then set a custom validity message on the second field.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How do you show an Apex error to the user?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Catch it, read the message from error.body and display a toast or inline text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: Why is reading the error message awkward?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because Salesforce returns different shapes for Apex, field and page level errors, so a helper is useful.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: Does lightning-record-form need manual validation?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. It validates through the UI API, so required fields and rules are handled for you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: What is a pattern attribute?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A regular expression the value must match, with message-when-pattern-mismatch for the error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Can you validate on blur rather than on change?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Handle the blur event and run the same check there.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced LWC Form Validation Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Scenario: a rule depends on data only Apex knows. How do you handle it?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Fetch the limit with a wire, check it in the browser for speed, then enforce it again in Apex on save.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: Why should the same rule exist in two places?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because the client check is for user experience and the server check is for integrity. An API call skips the browser entirely.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How do you handle asynchronous validation, such as a duplicate check?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call Apex on blur, await the result, then set the custom validity based on the answer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: Scenario: the field stays red after the user fixes it. What is wrong?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">setCustomValidity was never cleared with an empty string on the successful path.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: How do you make errors accessible?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let the base components render them, since they wire up aria attributes and announce changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q22: How do you test validation with Jest?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Render the component, set values, dispatch change events and assert on the validity state and rendered messages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q23: How do you surface duplicate rule errors?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Catch the DML exception, read the field errors from the output and show them against the right field.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q24: What is the risk of heavy validation on every keystroke?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Slow typing and wasted Apex calls. So debounce the check or run it on blur instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q25: Does LWC Form Validation work inside a Flow screen?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. The component validates the same way, and Flow blocks Next when you report an invalid field.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q26: How do you validate a lightning-combobox?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same methods apply, since it also supports checkValidity and setCustomValidity. So the pattern stays consistent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q27: Scenario: a user pastes a value and no check runs. Why?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The handler is probably on keyup rather than change. Therefore use the change event, which fires on paste too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q28: Should LWC Form Validation block the save button?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Disabling the button hides why it is disabled. So leave it enabled, run the check on click and show the messages instead.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q29: How much validation belongs in the browser?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enough to stop obvious mistakes quickly. However, anything that protects data must also live in Apex or a validation rule.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/lwc\/guide\/data-form-custom-validation.html\" target=\"_blank\" rel=\"noopener noreferrer\">LWC Guide: Input Validation<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/component-library\/bundle\/lightning-input\/documentation\" target=\"_blank\" rel=\"noopener noreferrer\">Component Reference: lightning-input<\/a><\/li>\n\n<li><a href=\"https:\/\/github.com\/trailheadapps\/lwc-recipes\" target=\"_blank\" rel=\"noopener noreferrer\">LWC Recipes: Error Handling<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>LWC Form Validation combines built in attributes with custom checks.<\/li>\n\n<li>checkValidity is silent, while reportValidity shows the message.<\/li>\n\n<li>setCustomValidity sets your message, and an empty string clears it.<\/li>\n\n<li>Server errors arrive in several shapes, so use a helper to read them.<\/li>\n\n<li>Always repeat the real rule in Apex or a validation rule.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we bring in outside code with <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-static-resources-libraries\/\">Static Resources and Third-Party Libraries<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LWC Form Validation covers two jobs: stopping bad input before it is sent, and showing a clear message when the server rejects a save.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2429,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96,243],"tags":[26,113],"swapz_series":[],"class_list":["post-2432","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","category-testing-debugging","tag-intermediate","tag-lwc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>LWC Form Validation and Custom Error Messages<\/title>\n<meta name=\"description\" content=\"Learn LWC Form Validation with built-in attributes, setCustomValidity, reportValidity, server errors and the most asked developer interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LWC Form Validation and Custom Error Messages\" \/>\n<meta property=\"og:description\" content=\"Learn LWC Form Validation with built-in attributes, setCustomValidity, reportValidity, server errors and the most asked developer interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-24T07:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T05:59:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"LWC Form Validation and Custom Error Messages Explained\",\"datePublished\":\"2026-10-24T07:30:00+00:00\",\"dateModified\":\"2026-11-15T05:59:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/\"},\"wordCount\":1574,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/lwc-form-validation-featured.png\",\"keywords\":[\"Intermediate\",\"LWC\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\",\"Testing and Debugging\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/\",\"name\":\"LWC Form Validation and Custom Error Messages\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/lwc-form-validation-featured.png\",\"datePublished\":\"2026-10-24T07:30:00+00:00\",\"dateModified\":\"2026-11-15T05:59:49+00:00\",\"description\":\"Learn LWC Form Validation with built-in attributes, setCustomValidity, reportValidity, server errors and the most asked developer interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/lwc-form-validation-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/lwc-form-validation-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"LWC Form Validation featured image: code editor card with setCustomValidity call\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/lwc-form-validation\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LWC Form Validation and Custom Error Messages Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LWC Form Validation and Custom Error Messages","description":"Learn LWC Form Validation with built-in attributes, setCustomValidity, reportValidity, server errors and the most asked developer interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/","og_locale":"en_US","og_type":"article","og_title":"LWC Form Validation and Custom Error Messages","og_description":"Learn LWC Form Validation with built-in attributes, setCustomValidity, reportValidity, server errors and the most asked developer interview questions.","og_url":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-24T07:30:00+00:00","article_modified_time":"2026-11-15T05:59:49+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"LWC Form Validation and Custom Error Messages Explained","datePublished":"2026-10-24T07:30:00+00:00","dateModified":"2026-11-15T05:59:49+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/"},"wordCount":1574,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-featured.png","keywords":["Intermediate","LWC"],"articleSection":["Salesforce","Salesforce Developer","Testing and Debugging"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/","url":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/","name":"LWC Form Validation and Custom Error Messages","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-featured.png","datePublished":"2026-10-24T07:30:00+00:00","dateModified":"2026-11-15T05:59:49+00:00","description":"Learn LWC Form Validation with built-in attributes, setCustomValidity, reportValidity, server errors and the most asked developer interview questions.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/lwc-form-validation-featured.png","width":2400,"height":1260,"caption":"LWC Form Validation featured image: code editor card with setCustomValidity call"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/lwc-form-validation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"LWC Form Validation and Custom Error Messages Explained"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=2432"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2432\/revisions"}],"predecessor-version":[{"id":3195,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2432\/revisions\/3195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2429"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2432"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}