How to restrict 50characters(letters,numbers,dashes and @ for Single line text variable

ch_naresh207
Giga Expert

How to restrict single line text variable for'' Limit to 50 characters.   Check that only letters, numbers, dashes, and underscores are entered''.

Please suggest me any one.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Naresh,



Here you go:



1) To limit 50 characters set max_length=50 in the attribute section for that catalog variable. so no need for regular expression for this.



max length catalog variable.JPG



2) Have onChange Catalog Client script to validate the values are number, dash, underscore etc. on that variable.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


return;


}



var validate = new RegExp('^[a-zA-Z0-9-_]+$');


// a-zA-Z means letter


// 0-9 means allow numbers


// - and _ means allow this characters


// try giving value with length as less than 50 and special character such as @ or ! and it should throw the alert message and clear the variable



var result = validate.test(newValue);



if(!result)


{


// value is invalid show alert message and clear the variable


alert('Please give only letters, numbers, dashes, and underscores');


g_form.clearValue('<variableName>'); // add variable name here


}



}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Naresh,



You would require a regular expression for this.


I would come back and post the solution in some time for this.



Regards


Ankur


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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Naresh,



Here you go:



1) To limit 50 characters set max_length=50 in the attribute section for that catalog variable. so no need for regular expression for this.



max length catalog variable.JPG



2) Have onChange Catalog Client script to validate the values are number, dash, underscore etc. on that variable.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


return;


}



var validate = new RegExp('^[a-zA-Z0-9-_]+$');


// a-zA-Z means letter


// 0-9 means allow numbers


// - and _ means allow this characters


// try giving value with length as less than 50 and special character such as @ or ! and it should throw the alert message and clear the variable



var result = validate.test(newValue);



if(!result)


{


// value is invalid show alert message and clear the variable


alert('Please give only letters, numbers, dashes, and underscores');


g_form.clearValue('<variableName>'); // add variable name here


}



}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Thank you


Ankur,

 

What if I wanted to set up the table column field attribute to validate that the form entry was in this format:
xxxx-xxxx-xxxx-xxxx and could not exceed 18 characters (allowing letters and numbers). This column in the table is a string type.

 

Thanks you for your time and assistance.

 

Larry