You need to sign in to do that
Don't have an account?
VVH
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
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
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!