Activity Forums Salesforce® Discussions How to validate fields in lightning component in Salesforce?

  • Saddam

    Member
    September 4, 2019 at 6:41 am

    Hi Nikita,

    Here is the example

    <aura:component >
    <center>
    <br/>
    <h5> <b> Age Validator </b> </h5> <br/>
    Enter your age: <ui:inputNumber aura:id="inputCmp"/> <br/>
    <ui:button label="Submit" press="{!c.validateAge}"/>
    </center>
    </aura:component>

     

    {
    validateAge : function(component) {
    var ageField = component.find("inputCmp");
    var ageValue = ageField.get("v.value");

    if(isNaN(ageValue) || ageValue == '')
    {
    ageField.set("v.errors", [{message:"Enter a valid age."}]);
    }
    else if(parseInt(ageValue) > 150)
    {
    ageField.set("v.errors", [{message:"This is not a valid age."}]);
    }
    else if(parseInt(ageValue) < 18)
    {
    ageField.set("v.errors", [{message:"Minimum age to submit this form should be greater or equal 18"}]);
    }
    else
    {
    ageField.set("v.errors", null);
    }
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos