How can I restrict special characters in string field in catalog item?

sr_surendra
Giga Expert

I don't want any special character to be added in string field in service catalog item.

4 REPLIES 4

Shishir Srivast
Mega Sage

Hello,


Please check if this helps.



Try this onchange catalog client script.



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


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


          return;


  }


var pattern = /^[0-9a-zA-Z]*$/;


    if(!pattern.test(newValue))


    {


          alert('type your appropriate comments here');


          g_form.setValue('variable_name', ''); // adjust field name accordingly if you want to blank it out.


  }


}


Hello Shishir,



Following is the script which I am currently using



function onSubmit() {


    //Type appropriate comment here, and begin script below


    var alpha = new RegExp("^[a-zA-Z0-9]*$");


  var BusinessCase=g_form.getValue('justification');


  //alert("value"+BusinessCase);


  var res=alpha.test(BusinessCase);


  if (!res) {


          // g_form.showFieldMsg('justification', 'Invalid input', 'error');


alert ("Special Charecters are not allowed on Justification");


return false;


  }


return;


}



This script do not allows "Space" in the string field


Could you please tell me what changes needs to be done in current script in order to run this if statement containes spaces


sr_surendra
Giga Expert

Dear All,



I have finalised my script as follows


this works for restricting special character as well as allowing spaces in between



function onSubmit() {


// Type appropriate comment here, and begin script below


    //var alpha = new RegExp("^[a-zA-Z0-9]*$");


var alpha = /^[\s0-9a-zA-Z]*$/;


  var BusinessCase=g_form.getValue('justification');


  //alert("value"+BusinessCase);


  var res=alpha.test(BusinessCase);


  if (!res) {


            //g_form.showFieldMsg('justification', 'Invalid input', 'error');


alert ("Special Charecters are not allowed on Business Case / Justification field");


return false;


  }


return;


}



Regards,


Surendra Deshmukh


Thank you for the update, i missed to see your earlier response.