Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The staff score and staff justification is an array value.

What I have tried:

$(document).ready(function(){	
	
	//STAFF
	$("#btnSubmit").click(function(e){
		e.preventDefault();
		var staff_score = $('#staff_score').val();
		var staff_justification = $('#staff_justification').val().trim();

		if (staff_score != '0'){
			var isValid = true;
				
			if (!staff_justification.length){
				isValid = false;
				alert('Please insert your justification');
			}
				
			if (isValid){
				if (confirm("Are you sure with this action?")){
					$(this).append('<input type="hidden" name="mode" value="submit" />');
					$('form').submit();
				}
			}
		}
		else{
			alert('Please insert staff score');
			}
	});
});
Posted
Updated 2-Jun-22 19:47pm

1 solution

As common trick in JS form validation :

function validate(){
if(test != expected_value){
....;
....;
alert("something went wrong");
return false; // to quit the validation pattern
}


_________

be careful about 'submit' use..
if your form already have a submit button, the form will be send twice. weird thing after that.

---------

length need a comparison with integer .. you test a metric, but call for boolean, Js is low type, sure the 0|false will pass but be careful.
write it in plenty word :
if(myarray.length == 0){...;}

_________

as global advice, write your code again,
by embeding all tests , you made a 'confusing' validation script.

take at :
step 1 : what to do. what is the code needed ?
step 2 : what to do. what is the code needed ?
step 3 : what to do. and so and so..


review your global code pattern :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900