Special characters not allowed and length of characters in single line text varaible
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 04:24 AM
Hi,
I have a requirement have a single line text variable. In that variable the length should be minimum 15 characters and maximum 30 characters and it should not allow special characters.
Can anyone help me how to achieve this in single line text field? If its is select box i can give as max_length=30; in variable attributes in default value but its singe line text.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 04:32 AM
Hi Dhana,
An onChange() Client script that runs on the single line text field as below should help.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var valueis = g_form.getValue('yoursinglelinetextfield');//replace field name
if (valueis.length < 15) {
alert('Minimum 15 characters required');
} else if (valueis.length > 30) {
alert('Maximum 30 characters required');
}
var specialCharRegex = /[~@|$^<>\*+=;?`')[\]]/;
if (specialCharRegex.test(valueis)) {
alert('No Special characters allowed');
g_form.setValue('yoursinglelinetextfield', ''); //replace field name
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 04:46 AM
Hi,
write a client-script onSubmit function and paste the below code which will not allow special characters.
1) Using indexOf function
function isSpclChar(){
var text = g_form.getValue("field_name");
var length=text.length;
if(length <15)
{
alert("Please enter 15 characters");
}
else if(length >30)
{
alert("Please enter below 30 characters");
}
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
if(FIELD.value.indexOf(iChars) != -1) { // FIELD = your desired field name
alert ("The box has special characters. \nThese are not allowed.\n");
g_form.setValue('field_name', '');
}
}
2) Using For Loop
function isSpclChar(){
var text = g_form.getValue("field_name");
var length=text.length;
if(length <15)
{
alert("Please enter 15 characters");
}
else if(length >30)
{
alert("Please enter below 30 characters");
}
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < FIELD.value.length; i++) { // FIELD = your desired field name
if (iChars.indexOf(document.qfrm.q.value.charAt(i)) != -1) {
alert ("The box has special characters. \nThese are not allowed.\n");
g_form.setValue('field_name', '');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 04:50 AM
Hi,
write a client-script onSubmit function and paste the below code which will not allow special characters.
function isSpclChar(){
var text = g_form.getValue("field_name");
var length=text.length;
if(length <15)
{
alert("Please enter 15 characters");
}
else if(length >30)
{
alert("Please enter below 30 characters");
}
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
if(FIELD.value.indexOf(iChars) != -1) { // FIELD = your desired field name
alert ("The box has special characters. \nThese are not allowed.\n");
g_form.setValue('field_name', '');
}
}
2) Using For Loop
function isSpclChar(){
var text = g_form.getValue("field_name");
var length=text.length;
if(length <15)
{
alert("Please enter 15 characters");
}
else if(length >30)
{
alert("Please enter below 30 characters");
}
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
for (var i = 0; i < FIELD.value.length; i++) { // FIELD = your desired field name
if (iChars.indexOf(document.qfrm.q.value.charAt(i)) != -1) {
alert ("The box has special characters. \nThese are not allowed.\n");
g_form.setValue('field_name', '');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2020 05:24 AM
Hey Dhana,
For doing this:
It should not allow special characters.
In Madrid and onwards there is a functionality called Validation Regex.
Refer the screenshot:
Define a regular expression (regex) to validate a variable value.
- Navigate to Service Catalog > Catalog Variables > Variable Validation Regex.
- Click New.
- On the form, fill the fields.
For creating your regex refer the link:
And for setting maximum length You can give it in Default value-->variable attribute->max_length=30
Mark Correct and Helpful if it helps!!!
Best Regards,
Namrata.