Skip to main content

Simple Form Validation for All Fields Required with Jquery

Simple Form Validation for All Fields Required

Function 1

function trythis() {
        var incomplete = $('.required').filter(function() {
                             return $(this).val() === '';
                         });
        if(incomplete.length) {
            alert('All fields are required');
            //to prevent submission of the form
            return false;
        }

     }




Function 2


function validateForm() {
  var isValid = true;
  $('.form_compete').each(function() {
    if ( $(this).val() === '' )
        isValid = false;
  });
  return isValid;
}

 


For select box the option value must be blank



html

 <form method="post" action="" id="form_compete" onsubmit="return trythis()"  >





Second Answer
http://jsfiddle.net/2KTkg/

Comments