Skip to content

Commit 8c05bbe

Browse files
authored
Use GlideModal instead of alert
Also tried to optimize repeated code
1 parent fd7563e commit 8c05bbe

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
function onChange(control, oldValue, newValue, isLoading) {
2-
if (isLoading || newValue == '') {
3-
return;
4-
}
5-
var cost = g_form.getValue('variable_name'); //update variable name used for currency
6-
cost = cost.trim();
7-
// first character should be dollar sign
8-
var firstChar = cost.substring(0,1);
9-
if (firstChar != '$') {
10-
alert ("Please enter cost in $0.00 format");
11-
g_form.setValue("daily_rate", oldValue);
12-
return;
13-
}
2+
if (isLoading || newValue == '') {
3+
return;
4+
}
5+
6+
var cost = g_form.getValue('variable_name'); //update variable name used for currency
7+
cost = cost.trim();
8+
// first character should be dollar sign
9+
var firstChar = cost.substring(0, 1);
10+
if (firstChar != '$') {
11+
validationAlert(oldValue);
12+
}
1413

15-
// characters after the $ sign should be numerics
16-
var costType = isNaN(cost.substring(1));
17-
if (costType == true) {
18-
alert ("Please enter cost in $0.00 format");
19-
g_form.setValue("daily_rate", oldValue);
20-
return;
21-
}
14+
// characters after the $ sign should be numerics
15+
var costType = isNaN(cost.substring(1));
16+
if (costType == true) {
17+
validationAlert(oldValue);
18+
}
2219

23-
// entered value should have a decimal point
24-
var num = cost.substring(1);
25-
if (num.indexOf('.') == -1) {
26-
alert ("Please enter cost in $0.00 format");
27-
g_form.setValue("daily_rate", oldValue);
28-
return;
29-
}
20+
// entered value should have a decimal point
21+
var num = cost.substring(1);
22+
if (num.indexOf('.') == -1) {
23+
validationAlert(oldValue);
24+
}
3025

31-
// there must be 2 digits only after the decimal
32-
var decNum = num.substring(num.indexOf('.')+1, num.length);
33-
if (decNum.length != 2) {
34-
alert ("Please enter cost in $0.00 format");
35-
g_form.setValue("daily_rate", oldValue);
36-
return;
37-
}
26+
// there must be 2 digits only after the decimal
27+
var decNum = num.substring(num.indexOf('.') + 1, num.length);
28+
if (decNum.length != 2) {
29+
validationAlert(oldValue);
30+
}
31+
}
32+
33+
function validationAlert(oldValue) {
34+
g_form.setValue("variable_name", oldValue);
35+
var gm = new GlideModal("glide_warn");
36+
gm.setTitle("Currency formatting problem");
37+
gm.setPreference("title", "Please enter cost in $0.00 format");
38+
gm.render();
39+
return;
3840
}

0 commit comments

Comments
 (0)