- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 08:46 AM
i have below requirement.
there are 3 ID fields where the length should not exceed 4000 characters and need to show error message when it is exceeding the limit.
I need to handle 3 fields in single validation by creating an array for above fields. can someone help me how to validate using client script.
Please help for minimising below script
code i written is on submit need to minimise below code .
var stud = g_form.getValue('student_id');
if (stud.length != '' && stud.length > 4000) {
g_form.showFieldMsg("student_id", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
return false;
}
var stude = g_form.getValue('student_id1');
if (stude.length != '' && stude.length > 4000) {
g_form.showFieldMsg("student_id1", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
g_form.addErrorMessage("You have exceeded the 4000 character limit . Please revise and submit.");
return false;
}
var studen = g_form.getValue('student_id3');
if (studen.length != '' && studen.length > 4000) {
g_form.showFieldMsg("student_id3", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
g_form.addErrorMessage("You have exceeded the 4000 character limit . Please revise and submit.");
return false;
}
Solved! Go to Solution.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 09:25 AM
Hello,
Please check if the below code looks good to you and try once:
function onSubmit() {
var isValid = true;
var fieldNames = {
"student_id": "Y",
"student_id1": "Y",
"student_id3": "Y"
};
for (var fname in fieldNames) {
var fieldValue = g_form.getValue(fname);
if (fieldValue && fieldValue.length > 4000) {
g_form.showFieldMsg(fname, "This field has a maximum character limit of 4000. Please revise and submit.", "error");
if (isValid)
isValid = false;
}
}
if (!isValid) {
g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
return false;
}
}
With Array:
function onSubmit() {
var isValid = true;
var fieldNames = ["student_id","student_id1","student_id3"];
for (var fname in fieldNames) {
var fieldValue = g_form.getValue(fieldNames[fname]);
if (fieldValue && fieldValue.length > 4000) {
g_form.showFieldMsg(fieldNames[fname], "This field has a maximum character limit of 4000. Please revise and submit.", "error");
if (isValid)
isValid = false;
}
}
if (!isValid) {
g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
return false;
}
}
Please mark this as helpful/correct, if it answer your question.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 08:54 AM
Hi,
use onSubmit client script
function onSubmit(){
var isValid = true;
var stud = g_form.getValue('student_id');
if (stud != '' && stud.length > 4000) {
g_form.showFieldMsg("student_id", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
g_form.addErrorMessage("You have exceeded the 4000 character limit. Please revise and submit.");
isValid = false;
}
var stude = g_form.getValue('student_id1');
if (stude != '' && stude.length > 4000) {
g_form.showFieldMsg("student_id1", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
g_form.addErrorMessage("You have exceeded the 4000 character limit . Please revise and submit.");
isValid = false;
}
var studen = g_form.getValue('student_id3');
if (studen != '' && studen.length > 4000) {
g_form.showFieldMsg("student_id3", "This field has a maximum character limit of 4000. Please revise and submit.", "error");
g_form.addErrorMessage("You have exceeded the 4000 character limit . Please revise and submit.");
isValid = false;
}
if(!isValid){
return false;
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 09:12 AM
could you please help me in form of adding above fields in an array and validating same fields in an array.
var arr=[ 'student_id' , 'student_id2' , 'stduent_id3' ];
then checking for validation?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2022 11:28 PM
Was this a requirement from customer to use array? I don't think so.
For your requirement I already shared the working script.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2022 07:06 AM
this is for my requirement where we have 10 fields need to validate for each length which does not exceed 255 char limit and also need to write in single script using arrays for all 10 field in an array. your script also worked... it was not from customer but from my tech team they want me to use array instead of validating for each field...
thanks
kaveri