Hello All,
Recently i was working with a very basic requirement where in i had to restrict user from entering some special character in a fields.
We had done this earlier with the below code , though the code is perfect but still has some undesired looping
Recently i was working with a very basic requirement where in i had to restrict user from entering some special character in a fields.
@$&*!?~^
#
>
<
|...etc
We had done this earlier with the below code , though the code is perfect but still has some undesired looping
var HouseNumber = this.GetFieldValue("House Number"); if ((HouseNumber != "")) { var RegillegalChars = "\\-)(,:;@*_=?.`<>[]{}!£$%^&*#/~|+' '"; for (c=0;c< RegillegalChars.length;c++) { fax_char = RegillegalChars.substring(c,c+1); if (Regno.indexOf(fax_char,0) != -1) { TheApplication().RaiseErrorText("'House Number' contains the "+"\'"+ fax_char +"\'"+" char. This is not allowed."); } }//for loop } |
charCodeAt(0), function that checks for the first character in a variable
and using ascii values for the variable rather then the char value, so the code modified as
var HouseNumber = this.GetFieldValue("House Number"); if(HouseNumber.charCodeAt(0) <= 47) { TheApplication().RaiseErrorText('Invalid Character!'); } |
we avoided multiple loops, and achieved the same functionality :-)
*I have used 47, as the ASCII values for special characters are less then 47 , for complete list see below
https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
Related Post, http://www.siebelfoundations.com/2019/08/handling-special-characters-in-siebel.html