Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Catalog Client Script - field length validation

brycefraser
Giga Contributor

Hi there!

I'm new to SNow and am trying to add field validation to one of our forms.

Basically, when a certain Service Company is selected, I want to check that the text entered into the Employee Number field is exactly 6 characters long.

So far I have been unable to get this working, I am not sure what is going on.


The type is set to "onSubmit" and this is the code I have (modified to remove company specific info):

function onSubmit() {

var payid = g_form.getValue('id') //get payroll id

var company = g_form.getvalue('service_company')           //get company

        if (company.match("Certain Company")){           //check to see if the company matches Certain Company

                  if(payid.match(/^\d{6}/)){           //check to see if payroll id matches 6 numbers long

                            alert('Please ensure the Payroll ID is 6 characters long')           //if it doesn't, display errors

  }}

}

Any help/suggestions would be much appreciated!

Thanks!

1 ACCEPTED SOLUTION

Harish Murikina
Tera Guru

Try this





function onSubmit()


{  


 


var payid = g_form.getValue('id') //get payroll id  




var company = g_form.getvalue('service_company')           //get company  




        if (company.match("Certain Company")) // Tell me whatyour trying to match here here is your doing wrong




      {           //check to see if the company matches Certain Company  


                  if(payid.length != 6)


                    {           //check to see if payroll id matches 6 numbers long  


                            alert('Please ensure the Payroll ID is 6 characters long')           //if it doesn't, display errors  


                            return false;


                  }


      }  


}  


View solution in original post

4 REPLIES 4

Harish Murikina
Tera Guru

Try this





function onSubmit()


{  


 


var payid = g_form.getValue('id') //get payroll id  




var company = g_form.getvalue('service_company')           //get company  




        if (company.match("Certain Company")) // Tell me whatyour trying to match here here is your doing wrong




      {           //check to see if the company matches Certain Company  


                  if(payid.length != 6)


                    {           //check to see if payroll id matches 6 numbers long  


                            alert('Please ensure the Payroll ID is 6 characters long')           //if it doesn't, display errors  


                            return false;


                  }


      }  


}  


Pradeep Sharma
ServiceNow Employee

Hi Bryce,



Here you go.


function onSubmit() {  


 


var payid = g_form.getValue('id') //get payroll id  


var company = g_form.getvalue('service_company')   //get company


  alert(company); //Check what value you are getting here..just for testing


      if(company == 'Certain Company'){           //check to see if the company matches Certain Company  


                  if(payid.length != '6'){           //check to see if payroll id matches 6 numbers long  


                            alert('Please ensure the Payroll ID is 6 characters long')           //if it doesn't, display errors  


  }}  


}  



brycefraser
Giga Contributor

Thanks everyone!



I've gotten it to work as needed now, there were a few things that were wrong, i wasn't ending my lines with ";", the value returned for Service Company wasn't referenced properly and finally, using != "6" was perfect!




Thanks again!


John Shores1
ServiceNow Employee

I know this is an old post, but can someone confirm that match() is supported within a client script?  I have a use case where I need to confirm a string contains a specific sequence of characters, if it does, I need to show a field and make it mandatory.  So, far, I'm getting an "unhandled GlideAjax exception" when running it. Thanks in advance!