I am facing a challenge with validating user input in an input box where alphanumeric values are allowed along with certain special characters. I need to display an error message if the user enters a special character that is not supported by my application.
var inputKey = "abcdfg34$@!"
My approach involves filtering out the special characters from the string first.
var inp = inputKey.replace(/[a-zA-Z0-9]/g, ''); // Now ip = @!
$scope.specialchar contains a list of allowed special characters
$scope.specialchar = [@,#,$,%,<,^,<];
for(var i in $rootScope.specialChar ){
if(( inp.indexOf($scope.specialChar[i].value) > -1 ))
{
$scope.charAllowedText = true;
count++;
}
}
if(count == 0) - display error messagae
The current code seems to be effective in detecting the occurrence of the first unsupported special character. In the given example, the character (!) is not on the approved list. However, when my string contains @! both conditions (count == 0) might fail as only one character is checked. I also aim to include which specific unknown special character was entered by the user in the error message.
I would appreciate any insights into what could be going wrong and how I can address this issue.