0

I am using the following React Function for email Regex, but it doesn't work.

When i try this email-address: [email protected] i get the the error: "Email is invalid"

Here is my FormValidation Function to check the values:

import { emailRegex } from "./regex";

export function validateEmail(email) {
  if (!email.length) {
    return {
      status: true,
      value: "Email is required"
    };
  } else if (!emailRegex.test(email)) {
    console.log(email);
    return {
      status: true,
      value: "Email is invalid"
    };
  }
  return {
    status: false,
    value: ""
  };
}

export function validatePassword(password) {
  if (!password.length) {
    return {
      status: true,
      value: "Password is required"
    };
  } else if (password.length <= 6) {
    return {
      status: true,
      value: "Password is invalid"
    };
  }
  return {
    status: false,
    value: ""
  };
}

and here is my emailRegex Function:

export const emailRegex = /^(([^<>()[]\\.,;:\s@\"]+(\.[^<>()[]\\.,;:\s@\"]+)*)|(\".+\"))@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

Where is the issue of that? Thanks for our help

1

1 Answer 1

3

export const emailRegex=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

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

1 Comment

the difference is that you left a \ backslash after the parentheses and in the square bracket. ie ()\[\]

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.