Jquery practice form validation
< ;form action="" method="post" id ="myform">
Jquery code
$(document).ready(
function() {
$("#myform ").submit(function(){
var username=$("#name").val();
var age=$("#age").val();
var sex =$("input[name ='sex'][checked]").val();
var address=$("#add option[selected]").val();
var size= $("input[name='checkbox'][checked]").size();
var favoriteArray=Array(size);
$("input[name='checkbox'][ checked]").each(function(index,docEl){
favouriteArray[index]=$(this).val();// or docEl.value
});
if(username= ="")
{
alert("Sex name cannot be empty! ");
$("#name").focus();
return false;
}
if(age=="")
{
alert("Age Cannot be empty");
$("#age").focus();
return false;
}
if(size==0)
{
alert( "You haven't chosen a hobby yet!");
$("input[name='checkbox']").get(0).focus();
return false;
}
for(var i=0;ialert(favouriteArray[i]);
}
alert('Submission successful!');
});
});