Add Error Class to all Inputs with Zero Values in a DataTable

Hello Experts!

I need help on the following:

Currently, I'm using onblur to do client-side validation on DataTable inputs using this:
$("body").on('blur', 'input[name^="cass"]', function() {
	checkCA(10);
});

$("body").on('blur', 'input[name^="exam"]', function() {
	checkExam(70);
});

function checkCA(max) {
	var currentInput = $(event.target);

	if ( currentInput.val() > max) {

		toastr.error('CA Scores cannot be greater than '+max);
		urrentInput.addClass('invalid');
		} else {
		currentInput.removeClass('invalid');
		}
};

function checkExam(max) {
	var currentInput = $(event.target);

	if ( currentInput.val() > max) {

		toastr.error('Exam Scores cannot be greater than '+max);
		currentInput.addClass('invalid');
		} else {
		currentInput.removeClass('invalid');
		}
};

Open in new window

Now, I want to check Zeros but not onblur. User may not click on a particular input so onblur will not work. I want the border highlighted after the submit button is clicked so as to draw user's attention to the Zero value.

Actually, Zeros are allowed but I still want to show them. If they left it on purpose, it's okay. But what if it was a mistake? That's why I want it highlighted. After all, all inputs will still be available after submit.

Thank you
LVL 1
Opeyemi AbdulRasheedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zakaria AcharkiAnalyst DeveloperCommented:
Hi AbdulRasheed,

Try to add the check inside the submit event, something like :

$("form").on('submit', function () {
    $('input[name^="cass"],input[name^="exam"]').each(function(){
         if( $(this).val() == 0 ){
              $(this).addClass('error_class');
        }
    });
});

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Opeyemi AbdulRasheedAuthor Commented:
Thank you so much, brother
Zakaria AcharkiAnalyst DeveloperCommented:
Glad to help brother.
Opeyemi AbdulRasheedAuthor Commented:
Please, do you suggest I change all other codes where I used same methods to populate DataTable?
I mean change this:
myTable.clear().draw();
myTable.rows.add(data).draw();

Open in new window

To:
myTable.clear();
myTable.rows.add(data.data);
myTable.draw();

Open in new window

?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.