how to stop the invalid characters while you submitting the UI page?

JMR2
Mega Expert

Hi all, I have a requirement 

could you please advise, I want to stop the users from entering special character in, field on a ui page.

So, the table below shows the characters we should allow, anything else should not be allowed.

Thanks In advance.

find_real_file.png

1 ACCEPTED SOLUTION

ChrisBurks
Mega Sage

Using the following regular expression globally should capture anything that is not in listed in your screenshot:

var regexp = /[^\u0000-\u007F]|[\||{|}|`|~]/;

Note the caret '^' when used inside brackets '[]' takes on a negation as opposed to when it's used outside of the brackets. Which then it means the start of the string. Also I used a unicode range here to capture the basic latin characters which includes numbers. 
So the above loosely means flag me if any character is NOT between the given unicode characters OR if it's a pipe "|", open curly "{", close curly "}", back tick "`", or tilde "~". Regular expression reference

Here is a basic example using an input:

<input id="test" onkeyup="doCheck(this)">
<script>
var t = document.getElementById("test");
var regexp = /[^\u0000-\u007F]|[\||{|}|`|~]/;

function doCheck(evt){
   var val = evt.value.match(regexp);
   if(val){
       alert("illegal character: '" + val[0] + "'")
   }
}
</script>

View solution in original post

3 REPLIES 3

Manas Kandekar
Kilo Guru

Hi JMR

Create function eg. functionName() in Client controller which returns true if your condition satisfied. 

then

<form action="" onsubmit="return functionName();">

 

If my answer helped you in any way, please then mark it correct and helpful.

Kind regards,
Manas

Ian Mildon
Tera Guru

This Community post should give you a good direction in creating your validation script:

https://community.servicenow.com/community?id=community_question&sys_id=0f468fa1db1cdbc01dcaf3231f96...

ChrisBurks
Mega Sage

Using the following regular expression globally should capture anything that is not in listed in your screenshot:

var regexp = /[^\u0000-\u007F]|[\||{|}|`|~]/;

Note the caret '^' when used inside brackets '[]' takes on a negation as opposed to when it's used outside of the brackets. Which then it means the start of the string. Also I used a unicode range here to capture the basic latin characters which includes numbers. 
So the above loosely means flag me if any character is NOT between the given unicode characters OR if it's a pipe "|", open curly "{", close curly "}", back tick "`", or tilde "~". Regular expression reference

Here is a basic example using an input:

<input id="test" onkeyup="doCheck(this)">
<script>
var t = document.getElementById("test");
var regexp = /[^\u0000-\u007F]|[\||{|}|`|~]/;

function doCheck(evt){
   var val = evt.value.match(regexp);
   if(val){
       alert("illegal character: '" + val[0] + "'")
   }
}
</script>