Sandeep Kumar6
Giga Guru

Use Below script on change of field value

 

This script will help you to restrict end user to provide currency value in "$0.00" format

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

    //Type appropriate comment here, and begin script below

    var cost_field = g_form.getValue('cost_field');

    cost_field = cost_field.trim();

    // first character should be dollar sign

    var firstChar = cost_field.substring(0, 1);

    if (firstChar != '$' || firstChar != 'second' || firstChar != 'third') {

        alert("Please enter cost_field in $0.00 format along with the currency symbol");

        g_form.setValue("cost_field", oldValue);

        return;

    }

    // characters after the $ sign should be numerics

    var cost_fieldType = isNaN(cost_field.substring(1));

    if (cost_fieldType == true) {

        alert("Please enter cost_field in $0.00 format along with the currency symbol");

        g_form.setValue("cost_field", oldValue);

        return;

    }

    // entered value should have a decimal point

    var num = cost_field.substring(1);

    if (num.indexOf('.') == -1) {

        alert("Please enter cost_field in $0.00 format along with the currency symbol");

        g_form.setValue("cost_field", 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_field in $0.00 format along with the currency symbol");

        g_form.setValue("cost_field", oldValue);

        return;

    }

}

Please hit like or mark helpful if it helped you.

Comments
Mark Roethof
Tera Patron
Tera Patron

Hi there,

Nicely described. Question though, what does this provide better/more than the out-of-the-box Validation Regex on Variables?

Kind regards,
Mark

Sandeep Kumar6
Giga Guru

HI Mark,

Thanks for letting me know about the regex , I actually was not knowing about this OOB rggex. So I have written this custom code. Thought one advantage to above code that anyone know having knowledge on Regex can also modify or alter according to their need. 

Regards

Sandeep Kumar

 

 

Mark Roethof
Tera Patron
Tera Patron

Still sometimes you can't fulfill the validation with the out-of-the-box Variable Regex. So it's still a good write up of how this could be done scripted.

Also see below 2 articles I wrote a while ago about Variable Regex:
- 2019-08-14 Regexes for Catalog Items Variable Validation, part 2 [Madrid]
- 2019-04-22 Service Portal Catalog Items: Regex Field Validation [Madrid]

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

jparman
Tera Guru

Hi !

 

will this work in Portal or workspaces? Thanks!

 

 

jparman
Tera Guru

Setting the oldValue doesnt seem to work in portal or workspaces. do you have an idea how to make this work?

Nick43
Tera Contributor
Version history
Last update:
‎03-02-2020 11:39 PM
Updated by: