mercredi 25 juin 2014

Validation best approach while looping fields


Vote count:

0




I have a save button handler inside which i check the employee name , skills and level for the employee which can be in multiple. I need the best possible way to do it as i am certain the way i do it looks really messy. Thanks


JS code:



$("#btnSave").click(function(){

var empName = $("#empName").val().trim(); // VALIDATE THIS
var skillArr = [];
var empObj = {};

if(empName != '')
return false;

$(".trSkillCls").each(function( index ) {
// VALIDATE FOR skill and level
if($(this).find('input[name=skill]').val().trim() == '' || $(this).find('select[name=ddlSkillLevel] option:selected').text().trim() == '')
return false;

skillObj = {
"skill" : $(this).find('input[name=skill]').val(),
"level" : $(this).find('select[name=ddlSkillLevel] option:selected').text()
};
skillArr.push(skillObj);
});

empObj = {
"empName" : $("#empName").val(),
"skillDetails" : skillArr
};

$.post( "indexBase.php",
$('#str').val(JSON.stringify(empObj)),
function(info){
var result = JSON.parse(info);


$( "#divEmpDetails" ).empty();

$("#divEmpDetails").append($("#tmplEmpDetails").render({data:result}));

// verify this callback for failures
});

$("#mainForm").submit( function() {
return false;
});

$('.trSkillCls').not(':first').remove();
$( "#reset" ).trigger( "click" );
});


asked 41 secs ago






Aucun commentaire:

Enregistrer un commentaire