Jquery submit button validation

Jquery submit button validation

Hi,

I have a jsp page where some rows are displayed to be pushed to database on clicking submit button. Before submitting the rows user has to select an option from dropdown list.

My validation should be 
1. If user selects no records & no option from drop down box,  alert with 'Message X'
2. If user selects at least one row but no option from drop down box, alert with 'Message Y'
3. If user selects at least one row and one option from drop down box submit the form.

Below is the code which is not working. Please advise what needs to be done extra?


$(document).on("click", ".submit", function(e) {// submit button
alert("submit button clicked");
e.preventDefault();
var userOption=$("#exitStatus option:selected").text(); //drop down option selected.
var selectedrows = jQuery("#gridView").jqGrid('getGridParam','selarrrow');  //get selected rows from gridview
var orderdetails=[];
var rowCount = selectedrows.length;
if(rowCount == 0 || userOption==''){
$("#messageBox").html("<h4>Please select atleast one record</h4>");
$("#messageBox").dialog("open");
}else{
for(var i=0;i<rowCount; i++) {
 
       var selectedRow = jQuery("#gridView").jqGrid('getRowData',selectedrows[i]);   //get the selected row
       OrderNo = selectedRow.OrderNo;  //get the OrderNo from selected row by column name
   OrderRefNo = selectedRow.OrderRefNo;  //get the OrderRefNo from selected row by column name
   taskName = selectedRow.taskName;  //get the taskName from selected row by column name
   falloutDate = selectedRow.falloutDate;  //get the falloutDate from selected row by column name
   orderState = selectedRow.orderState;  //get the orderState from selected row by column name
   orderHistory=selectedRow.orderHistory;  //get the orderHistory from selected row by column name
   var order= {"Operation":userOption,"OrderNo":OrderNo,"OrderRefNo":OrderRefNo,"taskName":taskName,"falloutDate":falloutDate,"orderState":orderState,"orderHistory":orderHistory};
   orderdetails.push(order);
   }
var timer = null;
$.ajax ({
    type:"POST",
url:'bulkview',
    dataType:'json',
    beforeSend:function() {
    var p=5;
    $('#trans').show();
    $("#progressBarWindow").dialog({
       dialogClass: "no-close",
           height: 150,
           width:500,
           autoOpen:true
       });
       $("#progressBar").progressbar({value:0});
            timer = setInterval(function(){
            if(p >= 100) p =0;
               //This animates the bar
               //$("#progressBar .ui-progressbar-value").animate({width: p+"%"}, 50);
               //This does static sets of the value
               $("#progressBar").progressbar({value:p});
               p = p +5;                
       },50);
     
    },
    complete: function() {
   
    },
    
    data: {"orderdetails":JSON.stringify(orderdetails)},
   
     success: function(data){                  
    $("#progressBar").progressbar({value:100});       
     $( "#progressBarWindow" ).dialog("close");
     clearInterval(timer);
    var status =  data.status; 
   
     setTimeout(function(){
    if(status=='success')
    {
     $("#messageBox").html('<h4>'+rowCount+'/'+rowCount+' records submitted successfully</h4>');
    }
    else
    {
      $("#messageBox").html('<h4>'+status+'</h4>');
          }
    $("#messageBox").dialog("open");
     },150);
     $('#trans').hide();
      },
       error: function(e){
        //alert('inside error'+e);
        $("#progressBar").progressbar({value:100});       
     $( "#progressBarWindow" ).dialog("close");
     clearInterval(timer);
     setTimeout(function(){
     $("#messageBox").html('<h4>Exception during the data persistance</h4>');
     $("#messageBox").dialog("open");
     },150);
     $('#trans').hide();
         }
      });
}
 });