onChange() to check field value

casey4man
Giga Contributor

Update 8/7:

I had to re-do the script because i was running into issues with the split. When someone used ':' to end their intro to the issue, it would not let them submit. It was also messing with older stories. So this is what I have now but I cannot understand why this will not let me submit at all now. Even when all of the fields are filled in.

function onSubmit () {

var req_keys = [

                          'Instance(s):',

                          'Record(s) used:',

                          'Impacted user group(s):',

                          'Logged in user to view/create issue:',

                          'Steps to reproduce:'

  ];

var array = [];

var desc = g_form.getValue('description');

for (var i = 0; i < req_keys.length; i++); {

if (desc.indexOf(array, req_keys[i]) > -1) {

alert('Please include all template fields in your description:\n\nInstance(s) ->\nRecord(s) used ->\nImpacted user group(s) ->\nLogged in user to view/create issue ->\nSteps to reproduce ->');

return false;

}

}

}

____________________________________________________________________________________________________________________________________________________  

Hi,

I need to check to see if the "description" field has been changed when a user logs and incident. I have 4 fields within the description field that are mandatory to complete before the record can be submitted. I am running into issues trying to figure out how to check if the description field has been filled out completely. I have tried to compare the empty value to the filled value but then i cant submit the form. Any help would be appreciated!

See screen shots below:

find_real_file.png

Here is the client script I currently have:

function onSubmit() {

var description = g_form.getValue('u_description');

g_form.getValue('u_description');

    //Type appropriate comment here, and begin script below

if ( description == 'Instance: +

'\n' + 'Record used:'

'\n' + 'Impacted user group:'

'\n' + 'Steps to reproduce:'); {

alert('Please fill out all description requirements and attach screenshots');

return false;

}

}

1 ACCEPTED SOLUTION

Please check if this helps.



function onSubmit() {


var i;


var description = g_form.getValue('u_description');


var dec_text = description.split('\n');


for(i = 0; i < dec_text.length; i++){


var description_text = dec_text[i].split(':');


if(description_text[1] == ""){


alert('Please fill out all description requirements and attach screenshots');


return false;


}


}


}        


View solution in original post

7 REPLIES 7

Gurpreet07
Mega Sage

There could be simpler way of doing this but i always used string functions for this. Something like below.



var desc = g_form.getValue('description');


var instance = desc.substring(desc.indexOf('Instance:')+9, desc.indexOf('Record used:')).trim() ;               //9 bz length of   'Instance:'


alert(instance) ;



var recordUsed= desc.substring(desc.indexOf('Record used:')+12, desc.indexOf('Record used:')).trim() ;             //12 bz length of   'Record used:'


alert(recordUsed) ;


antin_s
ServiceNow Employee
ServiceNow Employee

Hi Casey,



I would suggest to create 4 separate fields to achieve this. With the separate fields, you have more control on validation.



Having all 4 fields in a single text field would make the maintenance and reporting really complex.



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


I would like to do that as well but the requirements dont allow for more fields - they just want the description field to be more clear.  


Please check if this helps.



function onSubmit() {


var i;


var description = g_form.getValue('u_description');


var dec_text = description.split('\n');


for(i = 0; i < dec_text.length; i++){


var description_text = dec_text[i].split(':');


if(description_text[1] == ""){


alert('Please fill out all description requirements and attach screenshots');


return false;


}


}


}