Email Validation (React Native). Returning the result as 'invalid' for all the entries

后端 未结 6 702
故里飘歌
故里飘歌 2020-12-30 21:34

I am trying to validate a users email, by checking it against an expression. But the result i am getting is invalid for all the entries.

UPDATED CODE

6条回答
  •  天涯浪人
    2020-12-30 22:15

    Having a function that validates the email (probably in some separate module as you are going to reuse it),

    export function validateIsEmail(email) {
      return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email);
    }
    

    You can validate email input,

    Live validation:

             {
                setEmail(emailText);
                setInlineValidations({
                  ...inlineValidations,
                  emailNotValid: !validateIsEmail(emailText),
                });
              }}
            />
    

    In this example setEmail and setInlineValidations are state setters defined by the useState hook, example const [email, setEmail] = useState('');, you can of course adapt this to your way of handling state.

提交回复
热议问题