Jquery form verification with condition if and nulls

I'm trying to get some conditional form verification to work but I'm struggling with it.

Here's the scenario: I have an optional text field (#hb) that can be left blank and the forms submits but if it is not blank then a date field (#hdate) must be entered before the form submits.

I've tried allsorts but the last one I've tried is (value !='') which I think is suppose to work out if #hb is blank.

Anyone have any pointers for me to go on?

Thanks all!

JQuery code
var bla = $('#hb').val();
var blaa = $('#hdate').val();

if (bla != '' && blaa == '') {
      $('#EC9').show();
      $('#Edialog').dialog('open');
      return false;
   } 
Edit: here's my full function
<script>
function validateForm() {
   var e = $('#Consultant');
   var x = e.val();
   var w = $('#out');
   var q = w.val();
   var r = $('#inpt');
   var t = r.val();
   var y = $('#Speciality');
   var u = y.val();


   var asa = $('#nahf').is(':checked');
   var asb = $('#ohf').is(':checked');
   var asc = $('#aelc').is(':checked'); 
   var asd = $('#aat').is(':checked');  
   var ase = $('#aqdp').is(':checked');

   var ped = $('#Pace').val();


    var bla = $('#hb').val();
    var blaa = $('#hdate').val();
    var blb = $('#pl').val();
    var blbb = $('#pdate').val();
    var blc = $('#inr').val();
    var blcc = $('#idate').val();
    var bld = $('#bilirubin').val();
    var bldd = $('#bdate').val();
    var ble = $('#crp').val();
    var blee = $('#cdate').val();



   if (x == null || x == "") {
      $('#EC1').show();
      $('#Edialog').dialog('open');
      return false;
   }

      if (q == null || q == "") {
      $('#EC2').show();
      $('#Edialog').dialog('open');
      return false;
   }

    if (t == null || t == "") {
      $('#EC3').show();
      $('#Edialog').dialog('open');
      return false;
   }

   if (u == null || u == "") {
      $('#EC4').show();
      $('#Edialog').dialog('open');
      return false;
   }

   if (asa == "0" & asb == "0" & asc == "0" & asd == "0" & ase == "0") {
      $('#EC5').show();
      $('#Edialog').dialog('open');
      return false;
   }

   if (ped == null || ped == "") {
      $('#EC8').show();
      $('#Edialog').dialog('open');
      return false;
   }

    if($(bla).val() != '' && $(blaa).val() == '') {
    $('#EC9').show();
    $('#Edialog').dialog('open');
    return false;
 } 

   if (blb != '' && blbb == '') {
      $('#EC9').show();
      $('#Edialog').dialog('open');
      return false;
   }

   if (blc != '' && blcc == '') {
      $('#EC9').show();
      $('#Edialog').dialog('open');
      return false;
   }

   if (bld != '' && bldd == '') {
      $('#EC9').show();
      $('#Edialog').dialog('open');
      return false;
   }

   if (ble != '' && blee == '') {
      $('#EC9').show();
      $('#Edialog').dialog('open');
      return false;
   }

}
</script>

Answers


/** In Front end Code / / For example onclick attribute must use return keyword then only it stop postback**/

<button type="button" id="btnSubmit" onclick="return validateForm()">Submit</button>

/** Jquery Code **/

function validateForm(){
    var isSubmit = true;
    if($("#hb").val().length != 0){
        if($("#hdate").val().length != 0){
            if(validateDateFormat($("#hdate").val())){
                   isSubmit=true;
            }
        }
        else
          {isSubmit=false;}
    }
    else
    { isSubmit=true; }
    return isSubmit;
}

function validateDateFormat(dateValue){
    var bool = false;
    var myDate = new Date();
    var bits = dateValue.split('/');
    myDate.setFullYear(bits[2], bits[1] - 1, bits[0]);
    if (myDate != "Invalid Date") {
        bool = true;
    }
    return bool;
}

/** In the date format should be in dd/mm/yyyy format. If you want any other format then change split function and reorder the setFullYear function **/


Need Your Help

Working with ChartObjects

excel vba excel-vba

I have a quick question regarding modifying the axes of a ChartObject with VBA in Excel.