Skip to main content

LWC Validation

Requirement :



Create a LWC which we will take information like First Name(Text) , Last Name(Text) and Phone(Tel) where these are mandatory fields. Give one button Named ‘Validate’ in the end of the form .On click of button it would show an error message if the mandatory fields are empty or the
phone is not in correct format.

Or in other words

Check validation

cmpValidateFields.html

<template>
    <lightning-card title="Check Validation" icon-name="custom:custom63">
        <lightning-input 
            label="First name" 
            class="validValue inputFirstName"                          
            minlength="3" 
            field-level-help="This First Name is required"                      
            message-when-bad-input="Your entry must be at least 3 characters."                     
            placeholder="First name" 
            message-when-value-missing="Please enter your First Name"                     
            required>
        </lightning-input>
        <br>
        <lightning-input 
            label="Last name" 
            class="validValue inputLastName"                           
            minlength="3" 
            field-level-help="This Last Name is required"                      
            message-when-bad-input="Your entry must be at least 3 characters."                     
            placeholder="Last name" 
            message-when-value-missing="Please enter your Last Name"                     
            required>
        </lightning-input>
        <br>
        <lightning-input
            label="Phone"
            class="validValue inputPhone"                       
            placeholder="0412 345 678"
            pattern="^(\s*\d\s*){10}$" 
            maxlength="12" 
            minlength="10"
            field-level-help="This Phone is required"
            message-when-bad-input="Phone number is not valid"           
            message-when-value-missing="Please enter your Phone Number"
            required>
        </lightning-input>
        <br>
        <center>
            <lightning-button                         
                variant="brand"
                label="Validate"                     
                onclick={handleValidation}>
            </lightning-button>
        </center>
    </lightning-card>     
</template>

cmpValidateFields.js

import { LightningElementtrack } from 'lwc';
export default class cmpValidateFields extends LightningElement {
    handleValidation() {
        const allValid = [...this.template.querySelectorAll('.validValue')]
            .reduce((validSoFarinputCmp=> {
                        inputCmp.reportValidity();
                        return validSoFar && inputCmp.checkValidity();
            }, true);        
    }     
}

cmpValidateFields.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>48.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
        <target>lightning__Tab</target>
      </targets>
</LightningComponentBundle>

Output :



Happy Learning !!

Comments

Popular posts from this blog

LWC Show Records In Table

Requirement : ‹ › Create a LWC component which will show the most recently created 10 Opportunity from the org and show them in table having Name , Close Date , Stage Name , Type and Create . or in other words Show Opportunity data in table format. cmpOpportunityRecords.html < template >      < lightning-card   title = "Opportunity Records"   icon-name = "standard:opportunity" >                  < lightning-datatable                            data = {data}                columns = {columns}                key-field = "Id" >          </ lightning-...

LWC Update Single Record Datatable

Requirement : ‹ › Create a LWC in which get the contact records in Datatable and Update single record. Or in other words Update Single Record through Datatable. cmpDatatableUpdateSingleRecord.html < template >           < lightning-card   title = "Update Contact Record"   icon-name = "custom:custom63" >          < div   class = "slds-m-around_medium" >              < template   if:true = {contact.data} >                  < lightning-datatable                      key-field = "Id"                    ...

LWC Decorators

Decorators : ‹ › In LWC decorators are used to add functionality to a property or function. There are three types of decorators : 1. @track 2. @api 3. @wire @track : It is used to make properties or methods in LWC is private. This properties is not accessible outside the LWC in which it is declared. @api : It is used to make properties or methods in LWC is public. This way they are accessible outside of the component in which they are declared. For ex : parent component can set the value of the properties in the child component. @wire : It is used to read the Salesforce data from apex class into LWC. It is generally used while making the server call to apex class. LWC Properties : There are two type of LWC properties. 1. Reactive Properties 2. Non-Reactive Properties Reactive Properties : In case of reactive properties , value of the properties change when the component re-render. There are two types of Reactive Properties. 1. Public properties...