- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2015 01:59 PM
Hi ServiceNow Community Developers,
I managed to figure this out using a script, here is a script that I wrote which works well for everything i wanted to validate for in the variable:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var cost = g_form.getValue('cost');
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("cost", 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("cost", 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("cost", 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("cost", oldValue);
return;
}
}
If there is a better to write this I will greatly appreciate it but the above script worked for me.
Thanks,
Johannes