How to restrict users to avoid entering characters other than alphabets and number in Salesforce Lightning Web Component?

How to restrict users to avoid entering characters other than alphabets and number in Salesforce Lightning Web Component?

pattern attribute in the lightning-input tag can be used o restrict users to avoid entering characters other than alphabets and number in Salesforce Lightning Web Component. Regular Expression should be used. In the following example, I have allowed alphabets and numbers alone.
Sample Code:
<template>
    <div class=”slds-box slds-theme–default”>
        <lightning-input
                        type=”text”
                        label=”String Only”
                        pattern=”[a-zA-z0-9]+”
                        message-when-pattern-mismatch=”Special Characters not allowed”>
        </lightning-input>
    </div>
</template>

Output:

Leave a Reply