2

I'm trying to create a nice util function to validate strings. Conditions are:

  1. Cannot be typeof "undefined"
  2. Cannot be null
  3. Must be a string i.e. not a number, object, array, etc.
  4. 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?

7 Answers 7

2

if (str1 != null) will coerce the str1 value, and will check both against null and undefined.

EDIT: ignore this part (Also, you don't need to use a regex to check if there's at least one character. You can use the string length. str1.length will evaluate to a falsey value if it's 0, otherwise it will evaluate to a truethy value.) You need a strict boolean value, so it should be str1.length > 0.

function isValidString(str1) {
  return str1 != null && typeof str1 === "string" && str1.length > 0;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Nice and terse!
@Sam Glad it helped! Thanks!
If you want the function to strictly return a boolean, you'll need to expand str1.length to str1.length > 0
@PatrickRoberts actually you're right... I'm editing it.. Thanks a lot!!
@PatrickRoberts You beat me to it. I was just posting this: return str1 != null && typeof str1 === "string" && str1.length > 0; will always return true or false
|
1

Why don't you add a typeof str1 === 'string' as shown below

if(typeof str1 === 'string' && str1.match(validRegEx)) {
  return true;
} else {
  return false;
}

Comments

0

That is because the match method is only available on strings, and you are attempting to call it on a number.

I would suggest re-writing the function to ensure that the argument is a string, rather than only checking for null or undefined values:

const isValidString = str1 => {
  if (!str1 || typeof str1 !== 'string') return false

  const validRegEx = /^[^\\\/&]*$/

  if (str1.match(validRegEx)) {
    return true
  } else {
    return false
  }
}

1 Comment

I think I like this the best. Thank you.
0

Convert number to string perform evaluating it

const isValidString = (str1) => {
str1 = str1.toString();
   if(typeof str1 === "undefined" || str1 === null) return false;

   const validRegEx = /^[^\\\/&]*$/;
   if(str1.match(validRegEx)) {
      return true;
   } else {
      return false;
   }
}

console.log(isValidString(3))

1 Comment

This is doing the opposite of what I want. If I enter 3 which is a number, I should get a false. By converting it toString() almost all values become acceptable which is not what I want.
0

Replace the if with following:

if(typeof str1 === "undefined" || str1 === null || typeof str1 !== 'string') return false;

Comments

0

Use RegExp#test instead, because your regular expression will always be available. Also, String#match returns an array (or null) which is not well-suited for boolean expressions and may produce unwanted results.

const isValidString = (str1) => {

  if (typeof str1 === "undefined" || str1 === null) return false;
  const validRegEx = /^[^\\\/&]*$/;
  
  if (validRegEx.test(str1)) {
    return true;
  } else {
    return false;
  }
}

console.log(isValidString(1));
console.log(isValidString('string'));

Note, your regexp is still wrong because it validates the number as true. You need to tweak it more.

Comments

0

Credit to djfdev on this one, all I've really done is simplify his answer to be more succinct.

const isValidString = str1 => {
    return typeof str1 === 'string' && /^[^\\\/&]*$/.test(str1);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.