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.

Field validation restrict field length and should not allow special characters except hyphen

sony8
Tera Contributor

Hi 

 

I am working on Onchange client script.

there is a field called description.

description should not allow special character but should allow hyphen.

it should only allow length = 32 char.

below is the script i have written.

 

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

    var regExp = /^[a-zA-Z0-9]+$/;
    if (!regExp.test(newValue)) {
        alert("Please Enter alpha-numeric value only");
        g_form.clearValue('description');
    }


}

 

 

 how to restrict length and allow hyphen ?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@sony8 

try this

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

    // Regular expression to allow alphanumeric characters and hyphens
    var regExp = /^[a-zA-Z0-9-]+$/;

    // Check if the new value matches the regular expression and has a length of 32 characters
    if (!regExp.test(newValue) || newValue.length != 32) {
        alert("Please enter an alphanumeric value with hyphens allowed, and ensure it is exactly 32 characters long.");
        g_form.clearValue('description');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

J Siva
Kilo Patron
Kilo Patron

Hi @sony8 
Below script can be used to fulfill your requirement.

 

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

    var regExp = /^[a-zA-Z0-9-]{32}$/;
    if (!regExp.test(newValue)) {
        alert("Please Enter alpha-numeric value only");
        g_form.clearValue('short_description');
    }

}

 

Explanation:

  • ^ asserts the position at the start of the string.
  • [a-zA-Z0-9-] matches any alphanumeric character or a hyphen.
  • {32} ensures that the previous character set is matched exactly 32 times.
  • $ asserts the position at the end of the string.

Ankur Bawiskar
Tera Patron
Tera Patron

@sony8 

try this

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

    // Regular expression to allow alphanumeric characters and hyphens
    var regExp = /^[a-zA-Z0-9-]+$/;

    // Check if the new value matches the regular expression and has a length of 32 characters
    if (!regExp.test(newValue) || newValue.length != 32) {
        alert("Please enter an alphanumeric value with hyphens allowed, and ensure it is exactly 32 characters long.");
        g_form.clearValue('description');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader