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

后端 未结 6 700
故里飘歌
故里飘歌 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 22:33

    Looks like a syntax error. You've got a nested function called validate directly inside the definition for go.

    As a general rule I would suggest to keep your indentation and curly-brackets consistent so these sort of errors are detectable at a glance-- when the brackets don't line up there's a problem.

    Then, there's a few things you might do to get this code working:

    • Remove the validate (email) line along with its accompanying close bracket
    • Reference email via this.state.email in go
    • Add an additional state variable to indicate if the email has been validated or not.

    Something like:

    this.state = {
     email :'',
     validated : false,
    }
    

    And...

    go = () => {  
            if (this.state.email.test(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)==0) {
                this.setState({ validated : true });
            } else {
                this.setState({ validated : false });
            }
        }
    

提交回复
热议问题