Is there a way to ensure that special characters are not entered in a single line text field

Cupcake
Mega Guru

I have a variable that is a single line text field. The customer wants to ensure that these conditions are met. Is there a way to accomplish this?

Must NOT contain the following printable characters: / : \ ~ & % ; @ ' " ? < > | # $ * } { , + = [ ].
Must NOT start with an underscore (_).

 

Thanks,

Karen

1 ACCEPTED SOLUTION

Here's one that does both AND wipes the field:

 

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

var nospec = g_form.getValue('nospecial');
var ucheck = nospec.startsWith("_");
var pattern = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/); //unacceptable chars
if (pattern.test(nospec)){
	alert("No special characters!");
	g_form.setValue('nospecial', '');
	return false;
}

	if (ucheck){
		alert("Cannot start with an underscore");
			g_form.setValue('nospecial', '');
		return false;
	}
	
	
	return true;

   
}

View solution in original post

9 REPLIES 9

Rajesh Mushke
Mega Sage
Mega Sage

Hey Kbrownibx,

 

did you tried with Regular expression? 

 

Let me know if you need more help

 

Thanks,

Rajashekhar Mushke

Community Leader - 18



Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

Hey Shane,

    I am trying this and I keep getting a Java console error and I am not sure why. Here is how I have the script.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var iChars = "~`!#$%^&*+=-[]\\\';,/{}|\":<>?";
if(!iChars.test(/\W/g)){
alert ("File name has special characters ~`!#$%^&*+=-[]\\\';,/{}|\":<>? \nThese are not allowed\n");
return false;
}
}

 

Thanks Karen.

I started from scratch based on the link I gave.  This seems to work for me ( nospecial being the name of the String variable I used):

 

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

var nospec = g_form.getValue('nospecial');
var pattern = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/); //unacceptable chars
if (pattern.test(nospec)){
	alert("No special characters!");
	return false;
}
	return true;

   
}

 

 

Note this doesn't help with the underscore piece.