Tuesday, 3 March 2020

ServiceNow OOB Regex Validation for Currency || Email || IP address || Letter || Mac address || Number || URL




As we are using Madrid version of ServiceNow. I have come across one OOB feature of ServiceNow in Madrid for validating  below
Currency || Email || IP address || Letter || Mac address || Number || URL

We have a table in ServiceNow called  Questions Regular Expression “question_regex”. Which store the regular expression in record format. See below.

OOB we only three validation but we can create as many as we want with the help of  Regex.


Once we create a record in Questions Regular Expression “question_regex” table we can put that validation while creating variable in Catalog item.
See below: This works on Single line text field.







No more writing lengthy script for validation.
Below are the Regex validation we can utilize  

- Currency ($)
^\$[0-9]*\.[0-9]{2}$
- Currency (€)
^\€[0-9]*\.[0-9]{2}$
- Email address
^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
- IBAN
^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}$
- IP address
^\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b$
- Letter (A-z)
^[A-Za-z]*$
- Mac address
^[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}$
- Zip Code (NL)
^[1-9][0-9]{3}[\s]?[A-Za-z]{2}$

Reference : 


More details on similar topic I have kept in portal  - https://www.servicenowpreparation.com/ you can visit.

Bel
Happy Learning

Regards
Sandeep Kumar


Monday, 2 March 2020

Custom Currency field Validation

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 != '$') {
        alert("Please enter cost_field in $0.00 format");
        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");
        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");
        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");
        g_form.setValue("cost_field", oldValue);
        return;
    }
}

Start and End Data validation according to Data format ServiceNow



Start Date Validation :
On Change Client Script :

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


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

        return;

    }

    if (g_form.getValue('start_date') != '') {

        var dFormat = g_user_date_format;

        var tFormat = g_user_date_time_format;

        var today = getDateFromFormat(formatDate(new Date(), dFormat), dFormat);

        var sDate = getDateFromFormat(g_form.getValue('start_date'), dFormat);

        if (today > sDate) {

            alert(getMessage("Select date in the future"));

            g_form.setValue('start_date', '');

        }

    }

}



End Date Validation :
On Change Client Script :
 


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

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

        return;

    }

    if (g_form.getValue('start_date') != '') {

        var dFormat = g_user_date_format;

        var tFormat = g_user_date_time_format;

        var end = getDateFromFormat(g_form.getValue('end_date'), dFormat);

        var today = getDateFromFormat(formatDate(new Date(), dFormat), dFormat);

        var sDate = getDateFromFormat(g_form.getValue('start_date'), dFormat);

        if (today > end) {

            alert(getMessage("Desired End Date has to be a date in the future"));

           g_form.setValue('end_date', '');

        } else if (sDate != '' && end < sDate) {

            alert(getMessage("End date should not be before Start date"));

            g_form.setValue('end_date', '');

        }

    } else {

        g_form.setValue('end_date', '');

        alert(getMessage('First please select Start Date Of Limit Change'));

    }

}