0

I have one Lightning input in which I have to show the error message if condition is not satisfied.

1.4 characters min

2.12 characters max

3.Allowed characters: Letters (A,B,M....26 capital letters of the English alphabet)

4.Digits (1,2,3....)

5.Dash ( - )

  1. It must contain at least 1 digit

Need to check and show different error message for different condition if not satisfied in Aura Component in JS. help with this how can I achieve this in aura

Controller:

  if( 4 > updatedAFName.length )
        {
            let errorMessage= "Le numéro d'adhésion ne peut pas contenir moins de 4 caractères";
            helper.showToast(component, event, helper,afLabel,errorMessage,"error"); 
             component.set("v.isInEditingMode", true);
             component.set("v.isInSavingMode", true);
        }
        else if(updatedAFName.length > 12 )
        {
            let errorMessage= "Le numéro d'adhésion ne peut pas excéder 12 caractères";
            let afLabel= $A.get("$Label.c.TM_B02_APPLICATION_FILE");
            helper.showToast(component, event, helper,afLabel,errorMessage,"error"); 
            
        }
        else{
             if(isChangeAFName) {
                 component.set("v.IsApplicationNumberModalOpen", true);
                 component.set("v.isChangeAFName", true);
                }
                component.set("v.isInEditingMode", true);
                component.set("v.isInSavingMode", true);
                //component.set("v.IsApplicationNumberModalOpen", true);
            }```
2
  • Please add the code that you already have to your question. Commented May 18, 2022 at 8:58
  • added the code , not getting how to check for alphabet and hiphen Commented May 18, 2022 at 11:55

1 Answer 1

0

To check length use below:

if(stringToCheck.length < 4 || stringToCheck.length > 12){
 //throw error
}

To not allow small letters a-z use below:

const regex = /[a-z]/;
if(regex2.test(stringToCheck)){
 //if a-z is entered throw error
 //this will allow A-Z, 0-9, '-'.... 
 //throw error
}

To must contain at least one digit use below:

const regex = /\d/;
if(!regex2.test(stringToCheck)){
 //if no digit is entered throw error
 //throw error
}
3
  • its is showing null when i inserted ( const regex = /\d/ ) in Aura Component's JS Commented May 18, 2022 at 13:59
  • It is working fine for me. Please recheck if you are not using same name for 2 different regex. Also, recheck if you are using correct string variable for comparison.
    – Sneh
    Commented May 18, 2022 at 14:30
  • ya working fine Commented May 19, 2022 at 11:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .