Validate Each Input Type Field For Null Values

During work, my requirement was to Validate Each Input Type Field For Null Values but I also don’t wanted to call one function for each input type field like

document.ready(function () {
  $('#input1').on('onchange', functionName);
  $('#input2').on('onchange', functionName);
  $('#input3').on('onchange', functionName);
});

Any my another requirement was that I need all input type fields except hidden fields, so in order to achieve this, I have created a new jquery function that filter the input types and get all those whose type is not hidden and after this I am getting validating value if its null or not. If the value is null then I return false else true.

Solution

function validateInputFields()
{
   $('input[type!=hidden]').each(function (index, element) {
      if (element.value === '') {
        return false;
      }
   });

   return true;
}

isAllFieldsValidated = validateInputFields();
if(!isAllFieldsValidated)
{
   alert('Your error message');
}

Hope you will enjoy this

Leave a Reply