Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

String Validation (starts with)

Derek10
Tera Expert

Greetings all,

I'm looking for some assistance on syntax for a series of start withs

IE if it startswith ABC, or XYZ or CAT.


I know it's a on change script for the variable, just not sure on how to match characters. (case insensitive)

Any thoughts?

1 ACCEPTED SOLUTION

drjohnchun
Tera Guru

Here's one way to do it using test():



if (/^(ABC|XYZ|CAT)/i.test(value)) {   /* process TRUE condition */   }   // starts with ABC or XYZ or CAT, ignore case



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Winner of November 2016 Members' Choice Award


View solution in original post

5 REPLIES 5

Raju Koyagura
Tera Guru

ServiceNow also supprots Regex so use SNC Regex API - ServiceNow Wiki


dvp
Mega Sage

Here is the sample script



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


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


          return;


    }



    //Type appropriate comment here, and begin script below


    if (newValue.match(/^ABC|XYZ|CAT/i))



      {


              return true;


      }


     


      else{


              alert('Fail');


              g_form.clearValue('You Field Name');


      }


             


}


This on-change client script worked perfectly for me - We have two fields on our work orders that require information from another system and folks were always entering the wrong things. Here's my version: 

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


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

        return;
    }

    //Type appropriate comment here, and begin script below
	
    if (newValue.match(/^SO/i))

    {

        return true;

    } else {

        alert('The Sales Order number must begin with SO...');

        g_form.clearValue('u_salesorder');

    }

drjohnchun
Tera Guru

Here's one way to do it using test():



if (/^(ABC|XYZ|CAT)/i.test(value)) {   /* process TRUE condition */   }   // starts with ABC or XYZ or CAT, ignore case



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Winner of November 2016 Members' Choice Award