Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 473.1k

How to make all texbox when leaving have one label error mes

Mar 24 2020 2:34 AM
Hi Team
 
I have 5 textbox and want to find a way to make them when leaving will have label error message. So far i have done the following and seem not winning, kind need of an assistance please mates.
    1. <div class="row">  
    2.                             <label for="Address">Address</label>  
    3.                             <div class="col-md-6 ">  
    4.                                 <div class="input-group pull-right">  
    5.                                                    
    6.                                     @Html.TextBoxFor(m => m.HomeMainModel.Address, new { @class = "form-control", type = "text", id = " street address", autofocus = "autofocus", placeholder = "Street Address", required = "required" })  
    7.                                     <div class="input-group-append">  
    8.                                         <div class="input-group-text">  
    9.   
    10.                                         </div>  
    11.                                     </div>  
    12.   
    13.                                 </div>  
    14.   
    15.                             </div>  
    16.                         </div>  
    17.                         <div class="col-md-6 col-sm-10 col-xs-10">  
    18.                             <label id="labelMessaget" class="text-danger" style="display:none"></label>  
    19.                         </div>  
    20.                         <hr />  
    21.                         <div class="row">  
    22.                             <div class="col-md-6 ">  
    23.                                 <div class="input-group pull-right">  
    24.                                                                      
    25.                                     @Html.TextBoxFor(m => m.HomeMainModel.Address, new { @class = "form-control", type = "text", id = "address", autofocus = "autofocus", placeholder = "Street Address Line 2", required = "required" })  
    26.                                     <div class="input-group-append">  
    27.                                         <div class="input-group-text">  
    28.                                         </div>  
    29.                                     </div>  
    30.                                 </div>  
    31.   
    32.                             </div>  
    33.                         </div>  
    34.   
    35.   
    36.        <div class="form-group">  
    37.                             <div class="input-group mb-2">  
    38.                                 <div class="input-group-prepend">  
    39.   
    40.                                 </div>  
    41.   
    42.                                 <div class="input-group col-md-7 col-md-offset-7 col-sm-7 col-xs-7">  
    43.                                     <div class="input-group pull-right">  
    44.                                                                        
    45.                                         @Html.TextBoxFor(m => m.HomeMainModel.City, new { @class = "form-control", type = "text", id = "city", autofocus = "autofocus", placeholder = "City", required = "required" })               
    46.   
    47.                                         @Html.TextBoxFor(m => m.HomeMainModel.State, new { @class = "form-control", type = "text", id = "state", autofocus = "autofocus", placeholder = "State/ Province", required = "required" })  
    48.   
    49.                                         <div class="input-group-append">  
    50.                                             <div class="input-group-text">  
    51.                                             </div>  
    52.                                         </div>  
    53.                                     </div>  
    54.                                 </div>  
    55.                             </div>  
    56.                             <div class="col-md-6 col-sm-10 col-xs-10">  
    57.                                 <label id="labelMessaget" class="text-danger" style="display:none"></label>  
    58.                             </div>  
    59.   
    60.   
    61. <div class="input-group mb-2">  
    62.                                 <div class="input-group-append">  
    63.   
    64.                                 </div>  
    65.                                 <div class="input-group col-md-7 col-md-offset-3 col-sm-2 col-xs-2">  
    66.                                     <div class="input-group pull-right">  
    67.                                                            
    68.                                                      
    69.                                         @Html.TextBoxFor(m => m.HomeMainModel.Code, new  
    70.                                    {  
    71.                                        @class = "form-control",  
    72.                                        type = "text",  
    73.                                        id = "postal code",  
    74.                                        autofocus = "autofocus",  
    75.                                        placeholder = "Postal/Zip Code"  
    76.   
    77.                                         })  
    78.                                                             
    79.                                         @Html.DropDownListFor(m => m.HomeMainModel.SelectedCountryId, this.ViewBag.CountryList as SelectList, new { @class = "form-control" })  
    80.                                         <div class="input-group-append">  
    81.                                             <div class="input-group-text">  
    82.   
    83.                                             </div>  
    84.                                         </div>  
    85.   
    86.                                     </div>  
    87.                                 </div>  
    88.                             </div>  
    89.                         </div>  
    90.                         <div class="col-md-6 col-sm-10 col-xs-10">  
    91.                             <label id="labelMessaget" class="text-danger" style="display:none"></label>  
    92.                         </div>  
    93.   
    94.   
    95.   
    96.         // function when leaving texbox for city, street address, street_address_line2, state_province.  
    97.   
    98.     $(function() {  
    99.     $('#City').blur(function() {  
    100.         var city = document.getElementById("City").value;  
    101.         var expr = /^([\w-\.]+)@@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;  
    102.         if (!expr.test(city)) {  
    103.             document.getElementById("labelMessaget").style.display = "inline";  
    104.         } else {  
    105.             document.getElementById("labelMessaget").style.display = "none";  
    106.         }  
    107.     });  
    108. });  
 

Answers (3)