Page 1 of 1

Confirmation Pop Up Box

Posted: 2020-06-14 16:13
by zibrahim
Hi there,
Need help with adding an additional confirmation pop up box if certain criteria met when user is trying to create or update a record.
Assuming that if "amount" field entered is equal or less than 0, then a confirmation box pop up asking if the user is sure of the value entered.
If user click OK, then proceed with creating or updating the record. If user click Cancel, then cancel the record creation or deletion to let user check the value again.
This pop up box should not appear if "amount" entered is more than 0.

Appreciate if someone can help with the js codes to be inserted in the hooks/tablename-dv.js file.

Thank you and have a nice day.

Zala.

Re: Confirmation Pop Up Box

Posted: 2020-06-14 18:05
by pfrumkin
Hi Zala,

I'm not a JS expert but I think something like below would work

Code: Select all

function checkAmount()
{
	// Check that the amount is > 0
	var amount = $j('#amount').text();  // verify the control ID (#amount) by doing View Source
	if ((amount != null) && (amount.trim().length > 0) && (parseInt(amount) > 0)) {
		return 1;
	}
	return 0;
}

$j('#insert').click(function(){
	if ( checkAmount() ) {
		alert('Amount must be greater than 0');
	}
})

$j('#update').click(function(){
	if ( checkAmount() ) {
		alert('Amount must be greater than 0');
	}
})
I didn't actually try to run this, I may have some syntax error.

You could also google and experiment with the onchange event rather than the form insert/update if that would be a better experience.

Good luck.

~Paul

Re: Confirmation Pop Up Box

Posted: 2020-06-15 12:11
by zibrahim
Hi Paul,
Thanks for the guidance. I have managed to come out with the following codes in hooks/table_name-dv.js file.

Code: Select all

function show_error(field, msg) {
	modal_window({
		message: '<div class="alert alert-danger">' + msg + '</div>',
		title: 'Error in ' + field,
		close: function () {
			$j('#' + field).focus();
			$j('#' + field).parents('.form-group').addClass('has-error');
		}
	});
	return false;
}

function show_error2(field) {
	$j('#' + field).focus();
	$j('#' + field).parents('.form-group').addClass('has-error');
	return false;
}

/* on inserting or updating record, validate the Premium value */
$j('#update, #insert').click(function () {
	/* Make sure premium, if provided, is numbers only and not empty */
	var premium = $j('#premium').val();
	if (isNaN(premium) || premium == '') {
		return show_error('premium', 'Premium value should be numbers only and not empty.');
		/* show confirmation pop up box if premium is less or equeal 0 */
	} else if (premium <= 0) {
		var result = confirm('Click OK to proceed with Premium value ' + premium);
		if (result === true) {
			return;
		} else {
			return show_error2('premium');
		}
	}
});
Next, I need to figure out how to prevent the confirmation pop up box from appearing when I want to update the record (other field update) which already with 0 or negative premium value.
Again, thanks Paul for showing the light.

Zala.

Re: Confirmation Pop Up Box

Posted: 2020-06-15 17:43
by pbottcher