Field should only have at least 1 letter and 1 number only

ss123
Tera Contributor

Hello everyone,

 

How can we restrict a field to only accept combination of letter and number values or  all letters only?

  • Should not accept if field purely contains numeric values
  • Should not accept if field contains any special characters

Basically at least 1 letter and 1 number or All letters only.

Sample: ABCGSM, GSMXWX1, AZXSSS, SVCSAL8

 

Using onChange client script.

Thanks !

1 ACCEPTED SOLUTION

Moin Kazi
Kilo Sage
Kilo Sage

Hi @ss123 ,

 

Please use below regEx for your field -

var regex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\s]*$|^[a-zA-Z\s]+$/;

 

You can create an OnChange Client script for your field -

For example, I have implemented it for the incident Short Description field. If the value fails validation, it will be cleared from the field.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return; // Skip validation if the field is empty or the form is loading
    }

    // Define the regex pattern
    var regex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\s]*$|^[a-zA-Z\s]+$/;

    // Test the new value against the regex
    if (!regex.test(newValue)) {
        g_form.clearValue('short_description'); // Optionally clear the field
    }
}

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.

 

Thank you!
Moin Kazi
www.linkedin.com/in/moinuddinkazi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

View solution in original post

5 REPLIES 5

palanikumar
Mega Sage

Hi,

You can use Regular Expression Field Validation for this requirement. Refer the below URL to create Regular Expression Field Validation:

https://www.servicenow.com/community/developer-articles/field-validation-regular-expressions/ta-p/23... 

 

For alphanumeric, use the below regular expression

^[a-zA-Z0-9]*$

Thank you,
Palani

Hi @palanikumar 

Thanks but it should not accept pure numbers.

Accepted value should be pure letters or combination of letter/number.

Hi@ss123 ,

 

You can try the below onChange client script.

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

    var numericPattern = /^(?!\d+$)(?=.*[A-Za-z])[A-Za-z0-9]+$/; // Regular expression pattern for numeric input

    if (!numericPattern.test(newValue)) {
        alert('Please enter only letters or a combination of letters and numbers. No pure numbers or special characters allowed.');
        g_form.setValue('your_field_value', '');
    }

}

 

https://regexr.com/

The above link will help you test your accepted inputs. Add the following expression:

/^(?!\d+$)(?=.*[A-Za-z])[A-Za-z0-9]+$/ and test it.

 

ashish_parab_0-1730103053553.png

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

Moin Kazi
Kilo Sage
Kilo Sage

Hi @ss123 ,

 

Please use below regEx for your field -

var regex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\s]*$|^[a-zA-Z\s]+$/;

 

You can create an OnChange Client script for your field -

For example, I have implemented it for the incident Short Description field. If the value fails validation, it will be cleared from the field.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return; // Skip validation if the field is empty or the form is loading
    }

    // Define the regex pattern
    var regex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d\s]*$|^[a-zA-Z\s]+$/;

    // Test the new value against the regex
    if (!regex.test(newValue)) {
        g_form.clearValue('short_description'); // Optionally clear the field
    }
}

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.

 

Thank you!
Moin Kazi
www.linkedin.com/in/moinuddinkazi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~