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.

Need help with client script for required field -- format as alphanumeric on demand form

mborkowski
Tera Contributor

Good Morning all--thanks in advance for your help!
Have a request to validate data on Demand record/form for a specific field.
There are existing values in field and we are not cleaning those up..
Sorry fairly new to scripting so be patient..


1. I would change the character limit from 100 to 4 on that field (however I cant because there are 1,000 records with garbage data in that field and we are leaving for now.. (not my call)
2. Required field format is a combo of letters & #'s (alphanumeric & always 4, example Z123)

 

this is my script however Im able to put any value in and save the record (see attached screenshot also)
or is there a better way to accomplish this? (this is the "on submit" client script, I would have to create an "on change script also)


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Type appropriate comment here, and begin script below

}/function onSubmit() {
var fieldName = 'u_car_numbr'; // 
var fieldValue = g_form.getValue(fieldName);
var regex = /^[a-zA-Z0-9]*$/;

if (!regex.test(fieldValue)) {
g_form.showFieldMsg(u_car_numbr, 'Only alphanumeric characters are allowed in this field.', 'error');
return false; // Prevents form submission
}
return true; // Allows form submission
}

 

1 ACCEPTED SOLUTION

Sarthak Kashyap
Mega Sage

Hi @mborkowski ,

 

I tried your problem in my PDI and it is working fine, Please check below onSubmit Client Script 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var fieldName = 'short_description'; // Replace with your actual field name I gave short_description to test
    var fieldValue = g_form.getValue(fieldName);

    var regex = /^[A-Za-z0-9]{4}$/;

    if (!regex.test(fieldValue)) {
        g_form.showFieldMsg(fieldName, 'Value must be 4 alphanumeric characters (e.g., Z123).', 'error');
        return false;
    }

    return true;
}

 

SarthakKashyap_0-1762271940370.png

 

 

Result: 

SarthakKashyap_1-1762271957196.png

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

View solution in original post

4 REPLIES 4

Renat Akhmedov
Kilo Sage

Hi mborkowski, 

Please test the following scripts - they should work. I see that also the name convention is not the best (like errors in u_car_numbr), but I left it as it is, so the scripts should work, 

Client script (onChange): 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading) return;

  var field = 'u_car_numbr';
  var value = (newValue || '').toString().toUpperCase();
  if (value !== newValue) g_form.setValue(field, value);

  var pattern = /^[A-Z][0-9]{3}$/;

  g_form.hideFieldMsg(field, true);

  if (value && !pattern.test(value)) {
    g_form.showFieldMsg(field, 'Format is wrong: letter (A–Z) + 3 digits, for example Z123', 'error');
  }
}

 

Client Script (OnSubmit): 

function onSubmit() {
  var field = 'u_car_numbr';
  var value = (g_form.getValue(field) || '').toString().toUpperCase();
  g_form.setValue(field, value);

  var pattern = /^[A-Z][0-9]{3}$/;

  if (!value) return true; // Allow empty value if the field is not mandatory

  if (!pattern.test(value)) {
    g_form.showFieldMsg(field, 'Format is wrong: letter (A–Z) + 3 digits, for example Z123', 'error');
    return false;
  }
  return true;
}

 
You should consider writing a Business Rule in the future as well, to prevent clients from bypassing the check via import/integration. But this is another story, haha. These client scripts also don't block updates to records with "old" garbage values ​​if the field hasn't been changed.

If it was helpful to you, please mark it with a thumb up. Thank you!

Best regards,
Renat Akhmedov

Chaitanya ILCR
Mega Patron

Hi @mborkowski ,

 

function onSubmit() {
    var fieldName = 'u_car_numbr'; 
    var fieldValue = g_form.getValue(fieldName);
    var regex = /^[a-zA-Z0-9]*$/; 

    if (!regex.test(fieldValue)) {
        g_form.showFieldMsg(fieldName, 'Only alphanumeric characters are allowed in this field.', 'error');
        return false; // Prevents form submission
    }
}

ChaitanyaILCR_0-1762269464653.png

 

replace your script with above and try

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

pavani_paluri
Tera Guru
Tera Guru

Hi @mborkowski 

Your current regex allows any alphanumeric string of any length. To enforce exactly 4 characters and start with a letter, update the regex like this:

function onSubmit() {
var fieldName = 'u_car_numbr';
var fieldValue = g_form.getValue(fieldName);
var regex = /^[A-Za-z][A-Za-z0-9]{3}$/; // Starts with letter, followed by 3 alphanumeric chars

if (!regex.test(fieldValue)) {
g_form.showFieldMsg(fieldName, 'Value must be 4 characters: start with a letter, followed by 3 alphanumeric characters (e.g., Z123).', 'error');
return false;
}
return true;
}

 

you can use similar on onchange as well.

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

Sarthak Kashyap
Mega Sage

Hi @mborkowski ,

 

I tried your problem in my PDI and it is working fine, Please check below onSubmit Client Script 

 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var fieldName = 'short_description'; // Replace with your actual field name I gave short_description to test
    var fieldValue = g_form.getValue(fieldName);

    var regex = /^[A-Za-z0-9]{4}$/;

    if (!regex.test(fieldValue)) {
        g_form.showFieldMsg(fieldName, 'Value must be 4 alphanumeric characters (e.g., Z123).', 'error');
        return false;
    }

    return true;
}

 

SarthakKashyap_0-1762271940370.png

 

 

Result: 

SarthakKashyap_1-1762271957196.png

 

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak