Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement validation for empty input field with jQuery?

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 294
    Comment on it

    Whenever we create a form, we always have a requirement to put validation for empty fields on some input fields. We usually implement this functionality by putting checks on "blur" and onFocus events of the input field.

    Example: In the below example I have put conditions on "blur" event for checking whether the input field is empty or not and also put condition on "focus" event to remove the error message when the particular input field gets focus.

    <html> 
      <head> 
        <title>Demo to put validation</title> 
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript"> 
           $(function(){
           $("#emailId,#firstName").blur(function() {
                var id = $(this).attr("id"); 
                if(id == "emailId")
            {
                    var emailId = $("#emailId").val();
                if(emailId == "")
                {
                    $('#validemailId_error').text("You can't leave this empty.");
                }
            }
                else if(id == "firstName")
                {
                    if($(this).val() == "")
                    {
                        $('#validfirstName_error').text("You can't leave this empty.");
                    }
                }
              });
    
          $('input[type="text"]').on("focus",function() {
              var id = $(this).attr("id");
              if(id == "emailId")
            {
                      if($(this).val() == "")
                {
                    $('#validemailId_error').text("");
                }
            }
              else if(id == "firstName")
              {
                  if($(this).val() == "")
                {
                    $('#validfirstName_error').text("");
                }
              }
    
            });
    
          });
    
        </script>
        <style>
        .error{color: #db4437;font-size:14px;}
    
        </style>
      </head> 
      <body> 
        <form class="form-horizontal">
          <div class="form-group form-group-space">
            <label for="inputEmail3" class="col-xs-4 control-label">First Name*</label>
            <div class="col-xs-8">
              <input type="text" class="form-control" name="firstName" id="firstName" placeholder="First Name">
              <span id="validfirstName_error" class="error"></span>
            </div>
          </div>
          <div class="form-group form-group-space">
            <label for="inputEmail3" class="col-xs-4 control-label">Email*</label>
            <div class="col-xs-8">
              <input type="text" name="emailId" id="emailId" class="form-control" placeholder="Email">
               <span id="validemailId_error" class="error"></span>
            </div>
          </div>
            </form>
      </body> 
    </html>
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: