function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VVHVVH 

Restricting special character in lwc

I have  a requirement to restrict special charcter in lwc. example code is
HTML:
<template>
    <lightning-input
        onchange={handleSpecialCharacter}        
    ></lightning-input>
</template>
JS:
handleSpecialCharacter(event){
        var specials=/[*|\":<>[\]{}`\\()';@&$]/;
        if (specials.test(event.target.value)){
            if(event.currentTarget.dataset.value === 'CustomField__c'){
                
                
                alert("Special Characters are not allowed ");
retrun false;
}
}
}

but after giving alert its still accepting special characters. anybody how to resolve this or how to ignore special chars
AbhinavAbhinav (Salesforce Developers) 
Hi Shreevarsha,

You can use similar input tag. 

<lightning-input label="Enter Something"
type="text"
pattern="[a-zA-z0-9]+"
message-when-pattern-mismatch="Special Characters not allowed">
</lightning-input>

https://www.infallibletechie.com/2020/10/how-to-restrict-users-to-avoid-entering.html

Hope above information helps, Please mark as Best Answer so that it can help others in the future

Thanks!