Wednesday, April 12, 2017

String Manipulation ( Change first letter to Uppercase )



Angular function for converting the first letter to uppercase
firstNameToUpperCase(value:string){
       //Check if the value exists
      if(value.length > 0)
      {
        //Fetch the first character and turn it into upper and leave the rest
        this.userModel.firstName=value.charAt(0).toUpperCase() + value.slice(1);

      }
      else
      {
          this.userModel.firstName=value;
      }
   }



Html file
  <form (ngSubmit)="addUsers()" #form="ngForm"  novalidate>
   <div class="container">
       <div class="row ">
           <div class="col-md-12" id="addUsers">
                <div class="col-md-4">
                   <label>First Name :</label><br/>
                   <!--$event passes the value !-->
                    <input type="text"  name="firstName" 
[ngModel]="userModel.firstName" (ngModelChange)="firstNameToUpperCase($event)" /> <br/>
                </div>
                
            </div>
     </div>
  </div>

Output

No comments:

Post a Comment