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
Vaibhab ShahVaibhab Shah 

how to allow only alphabet in lightning input text

Hi,
I have used a lightning input text. I want to allow only alphabet and restrict numbers, special characters and space.

Thnx,
Vai
Deepali KulshresthaDeepali Kulshrestha
Hi Vaibhab,

I've gone through your requirement and I came across a simpler way using the APEX controller extension class.
I used the following code in the extension class to check if the value is numeric or not.


Apex Controller>>>

boolean check = pattern.matches('[a-zA-Z]+',stringname);

if (check == true) {

the error message you want to display

}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Malika Pathak 9Malika Pathak 9

Hi Vaibhav,

Please try the Validation rule something like this 
NOT(REGEX(
 your field name ,"^[a-z  A-Z]*$")))

If you find this helpful mark it as the best answer

Suraj Tripathi 47Suraj Tripathi 47

Hi Vaibhav, 

You can try this:

Component

 <lightning:input aura:id="field" label="Client#2 Name" value='{!v.LeadObj.Name2__c}' messageWhenPatternMismatch="Please specify a valid Name"
                                                 pattern="[a-zA-Z]*"/>


Controller:

 var allValid1 = component.find('field').reduce(function (validSoFar, inputComponent){
            inputComponent.reportValidity();
            return validSoFar && inputComponent.checkValidity();
        }, true);
        if(allValid1) {
       // write your code here 
        }
        else {
            alert('Please update the invalid form entries and try again.');
        }

 

Please mark it as Best Answer If it helps!

Thanks