- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2015 09:07 PM
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!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2015 09:27 PM
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;
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2015 09:27 PM
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;
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2015 09:29 PM
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
}}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2015 10:05 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2019 09:48 AM
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!