2

Hi I am hoping this may be and easy one for some of you.

Essentially I am just asking if it is possible to use a regex match statement within a if statement.

I have used some in my Formik validation schema but am not sure if it is possible to use within an if statement.

This is my if statement

if (this.state.email.length < 8 || this.state.password.length < 8)

I would like to include something along the logic of

 .matches(/(?=.*outlook)/)

Is this possible ?

1

2 Answers 2

1

I think what you're looking for is the regex .test() method. It applies a regex to a string and returns true if it matches, or false if it doesn't. See here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Sign up to request clarification or add additional context in comments.

Comments

0

you can use match to validate regex expression in javascript, this is an example of email validation :

function ValidateEmail(inputText) {
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  if (inputText.match(mailformat)) return true;
}

2 Comments

Why if (text.match(regex)) return true than return regex.test(text)?
Thanks you are a hero. I only used a simple /@/ as my regex as I feel deeper validation for an email is not needed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.