Friday, 23 October 2015

How to validate a field value in lighting component

       Validating form is more over important in UI development. Here is the simple example for validating a number field in lightning component app. 

Please take a look on the below link, if you don't know about creating lighting component.

Lightning Component Code:

  1. <aura:component >
  2.     <center>
  3.         <br/>
  4.         <h5> <b> Age Validator </b> </h5> <br/>
  5.         Enter your age: <ui:inputNumber aura:id="inputCmp"/> <br/>
  6.         <ui:button label="Submit" press="{!c.validateAge}"/>
  7.     </center>
  8. </aura:component>

Component Controller Code:

  1. {
  2.     validateAge : function(component) {
  3.         var ageField = component.find("inputCmp");
  4.         var ageValue = ageField.get("v.value");
  5.        
  6.         if(isNaN(ageValue) || ageValue == '')
  7.         {
  8.             ageField.set("v.errors"[{message:"Enter a valid age."}]);
  9.         }
  10.         else if(parseInt(ageValue) > 150)
  11.         {
  12.             ageField.set("v.errors"[{message:"This is not a valid age."}]);
  13.         }
  14.         else if(parseInt(ageValue) < 18)
  15.         {
  16.             ageField.set("v.errors"[{message:"Minimum age to submit this form should be greater or equal 18"}]);
  17.         }
  18.         else
  19.         {
  20.             ageField.set("v.errors"null);
  21.         }
  22.     }
  23. }

Output:


1 comment:

  1. can we Increase that error message size?..if its how can we do that??

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...