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

后端 未结 6 699
故里飘歌
故里飘歌 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:07
    validate = (text) => {
    console.log(text);
    let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
    if(reg.test(text) === false)
    {
    alert("Email is Not Correct");
    this.setState({email:text})
    return false;
      }
    else {
      this.setState({email:text})
      alert("Email is Correct");
    }
    }
    
    
    You can put this function validate in onChangeText propert of TextInput
    
    0 讨论(0)
  • 2020-12-30 22:10
       isEmailValid = () => {
        const expression = /(?!.*\.{2})^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([\t]*\r\n)?[\t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([\t]*\r\n)?[\t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
        return expression.test(String(this.state.email_id).toLowerCase())
    }
    
    0 讨论(0)
  • 2020-12-30 22:12

    Ok I got the code working, below you can take the look for validating the email on each user input :

    1. Your function part:
    validate = (text) => {
      console.log(text);
      let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
      if (reg.test(text) === false) {
        console.log("Email is Not Correct");
        this.setState({ email: text })
        return false;
      }
      else {
        this.setState({ email: text })
        console.log("Email is Correct");
      }
    }
    
    1. You TextInput Component:
    <TextInput
      placeholder="Email ID"
      onChangeText={(text) => this.validate(text)}
      value={this.state.email}
    />
    
    0 讨论(0)
  • 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:

            <TextInput
                  ...
              onChangeText={emailText => {
                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.

    0 讨论(0)
  • 2020-12-30 22:23

    I've created a Helper class and doing like this:

    export default class Helper {
    
         static isEmailValid(email) {
            let reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
            return reg.test(email) == 0;
       }
    }
    

    Call this method by:

    if (Helper.isEmailValid(this.state.emailText)) {
        console.log("Email is correct");
    } else {
        console.log("Email is not correct");
    }
    
    0 讨论(0)
  • 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 });
            }
        }
    
    0 讨论(0)
提交回复
热议问题