I'm trying to create a nice util function to validate strings. Conditions are:
- Cannot be
typeof "undefined" - Cannot be
null - Must be a string i.e. not a number, object, array, etc.
- Must have at least one character in it. So a string with value of
''would be invalid.
I found some pointers on this that suggest using RegEx is the way to do it but the following is not working when I give it a numeric value.
Here's what I have so far:
const isValidString = (str1) => {
if(typeof str1 === "undefined" || str1 === null) return false;
const validRegEx = /^[^\\\/&]*$/;
if(str1.match(validRegEx)) {
return true;
} else {
return false;
}
}
As I said, if send const str1 = 3; to this function, I get an error that reads:
"TypeError: badString.match is not a function
What am I missing?