miranal
miranal

Reputation: 37

Regex for *%#:& including white space in react native?

In react native using textBox I need validation, showing if the user enters whitespace or *%#:& error will pop up with highlighting the text box red.

My code -

_validateInput = (s) => {
  let regSpace = new RegExp(/\s/); // what should i add to check *%#:& currently its checking only white space
  // Check for white space and (*%#:&)
  if (regSpace.test(s)) {
    this.setState({ error: true });
    return false;
  } else {
    this.setState({ error: false });
  }
  return true;
};

Upvotes: 0

Views: 1569

Answers (2)

Shivam
Shivam

Reputation: 2359

You can try this,

const name = 'Hi my name is john'
name.replace(/*%#:&\s/g, '') //Himynameisjohn

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521103

You may use the following character class:

[*%#:&\s]

Your updated code:

_validateInput = s => {
    if (/[*%#:&\s]/.test(s)) {
        this.setState({ error: true });
        return false;
    }
    else {
        this.setState({ error: false });
        return true;
    }
}

Upvotes: 0

Related Questions