To restrict special characters in a Single Line Textbox
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
in Choice box -To restrict special characters in a Single Line Textbox?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @25421
You can use client scripts with Regex to validate user input. Please share more details about where you're planning to implement this validation.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
If you know what all characters to restrict, a simp[le way would be using a client script as below
var string = g_form.getValue('field_name');
if (string.indexOf("#") > -1 || string.indexOf("*") > -1) { //here its restricting # and *, tyou can add more
alert("Please do not use the # or * characters");
g_form.clearValue('field_name');
}
Below is a more elaborate solution using regex
Solved: Restriction validation for single line text field - ServiceNow Community
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @25421 ,
You can create an onChange client script that triggers whenever the value of the Single Line Text field changes.
var regex = /[!@#$%^&*(),.?":{}|<>]/g;
if (regex.test(newValue)) {
alert('Special characters are not allowed.');
g_form.setValue(control.name, oldValue);
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/