How to put field validation on ui:inputtext in javascript using lightning component?

nitish picture nitish · Dec 9, 2015 · Viewed 8.5k times · Source

How to put field validation on ui:inputtext in JavaScript using lightning component?

Below is my html code:

<ui:inputText 
      class="slds-form-element__control slds-input" 
      value="{!v.CustomerPo}" 
      aura:id="customer_po" 
      maxlength="35"/>

kindly reply

Answer

pfernandez picture pfernandez · Jun 30, 2016

The above answer it's ok using the isNaN() from JS, but you can do the following if you define a helper method that can be used in the same kind of inputs, from other components. So, also using utilities from the framework:

// HELPER
function : validateNumber (component, auraId){
    var value = component.find("auraId").get("v.value");
    return $A.util.isNumber(value);
}

// CONTROLLER
function : saveButton (component){

    if (!helper.validateNumber(component, 'customer_po')){
        component.set("v.errorMessage", 'Not a numeric value');
    } else {
        component.set("v.errorMessage", '');
    }
}