-
Notifications
You must be signed in to change notification settings - Fork 504
/
currency_validation.js
38 lines (35 loc) · 1.1 KB
/
currency_validation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cost = g_form.getValue('variable_name'); //update variable name used for currency
cost = cost.trim();
// first character should be dollar sign
var firstChar = cost.substring(0,1);
if (firstChar != '$') {
alert ("Please enter cost in $0.00 format");
g_form.setValue("daily_rate", oldValue);
return;
}
// characters after the $ sign should be numerics
var costType = isNaN(cost.substring(1));
if (costType == true) {
alert ("Please enter cost in $0.00 format");
g_form.setValue("daily_rate", oldValue);
return;
}
// entered value should have a decimal point
var num = cost.substring(1);
if (num.indexOf('.') == -1) {
alert ("Please enter cost in $0.00 format");
g_form.setValue("daily_rate", oldValue);
return;
}
// there must be 2 digits only after the decimal
var decNum = num.substring(num.indexOf('.')+1, num.length);
if (decNum.length != 2) {
alert ("Please enter cost in $0.00 format");
g_form.setValue("daily_rate", oldValue);
return;
}
}