Reputation: 341
i have this code using javascript. where i use for loop in evaluating the text box if it has content or none.
this is the code:
function validateTable()
{
var rowCount = $('#mhTable >tbody >tr').length;
//alert(rowCount);
for(var ctr = 1; ctr <= rowCount; ctr++)
{
if( $('input[name^="process['+ctr+']"]').val() != '' )
{
continue;
}
else if( $('input[name^="process['+ctr+']"]').val() == '' )
{
messagealertManhourCheck("Error","Please fill out the form");
}
}
SaveSavingsData();
}
my problem is when there are many textboxes and if one of the textbox do not have any content. it will still go in the function saveSavingsData();
but it must be error.
Upvotes: 0
Views: 50
Reputation: 1785
in your code, it will always execute the SaveSavingsData(), if you want to stop execute if a input value is '',you can:
function validateTable()
{
var rowCount = $('#mhTable >tbody >tr').length;
//alert(rowCount);
for(var ctr = 1; ctr <= rowCount; ctr++)
{
if( $('input[name^="process['+ctr+']"]').val() != '' )
{
continue;
}
else
{
messagealertManhourCheck("Error","Please fill out the form");
return;
}
}
SaveSavingsData();
}
Upvotes: 1
Reputation: 8291
You do not need the else
statement with continue
, and you can use an additional variable for checking if there are any empty textboxes:
function validateTable()
{
var rowCount = $('#mhTable >tbody >tr').length;
var isValid = true;
//alert(rowCount);
for(var ctr = 0; ctr < rowCount; ctr++)//any reason your loop was starting from 1 ?? I changed it to 0
{
if( $('input[name^="process['+ctr+']"]').val() == '' )
{
isValid = false;
messagealertManhourCheck("Error","Please fill out the form");
}
}
if(isValid)
SaveSavingsData();
}
Upvotes: 0
Reputation: 1724
function validateTable() {
var rowCount = $('#mhTable >tbody >tr').length,
hasErrors = false;
for(var ctr = 1; ctr <= rowCount; ctr++) {
if( $('input[name^="process['+ctr+']"]').val() == '' ) {
messagealertManhourCheck("Error","Please fill out the form");
hasErrors = true;
}
}
if (!hasErrors) {
SaveSavingsData();
}
}
Upvotes: 2