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 ?

coderStew
  • 137
  • 2
  • 13
  • Does this answer your question? [Check whether a string matches a regex in JS](https://stackoverflow.com/questions/6603015/check-whether-a-string-matches-a-regex-in-js) – VLAZ Jan 10 '20 at 09:34

2 Answers2

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

Jayce444
  • 8,725
  • 3
  • 27
  • 43
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;
}
walid sahli
  • 406
  • 3
  • 11
  • Why `if (text.match(regex)) return true` than `return regex.test(text)`? – VLAZ Jan 10 '20 at 09:43
  • Thanks you are a hero. I only used a simple /@/ as my regex as I feel deeper validation for an email is not needed. – coderStew Jan 10 '20 at 09:58