Are you looking to restrict input to English letters a-z (both lowercase and uppercase), numbers, spaces, '_', and '-'? Keep in mind that allowing these specific characters is different from disallowing others like '☃' which might not fall within the set of characters you specified but could still be invalid for your intended use case.
If you indeed only want to permit the English alphabet, numbers, spaces, '_', and '-', you can implement the following RegExp pattern along with a conditional check:
var reg = /^[a-zA-Z0-9 \-_]+$/;
if (reg.test(inputString)) {
// String is acceptable
}
This regular expression ensures that all characters in the string are one or more occurrences of the allowed characters within the defined range.
To specifically disallow the characters mentioned in your query, you can utilize the following approach:
var reg = /[\<\>\:\,\/\\\|\?\*\#]/;
if (!reg.test(inputString)) {
// The string meets requirements
}
This code snippet searches for any of the listed characters (each escaped with a \
) and deems the string invalid if any are found. Therefore, the string is considered valid only if the test fails, indicated by the presence of !
before the test.