电子邮件验证(React Native).将结果返回为所有条目的"无效"

Avi*_*ati 17 javascript validation react-native react-native-ios

我试图通过对表达式进行检查来验证用户的电子邮件.但我得到的结果对所有条目都无效.

更新的代码

class dummytest extends Component{

  constructor(props){
    super(props);
    this.state = {
                email :'',
                validated: false ,
                 }
  };

go = () => {
           const reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
           if (reg.test(this.state.email) === true){
               alert( valid);
           }
           else{
               alert();
           }
 }
  render(){
       return(
         <View style={{alignSelf:'center',marginTop:100}}>
              <TextInput autoCapitalize="none" autoCorrect={false} style={{height:20,width:200,backgroundColor:'blue'}} value={this.setState.email}/>

              <Button onPress={this.go.bind(this)}>
                 <Text> GO </Text>
              </Button>
          </View>

       );
    }
}
Run Code Online (Sandbox Code Playgroud)

Nee*_*ala 44

好的,我的代码正常工作,下面你可以看看验证每个用户输入的电子邮件.

  1. 你的功能部分:

    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");
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 你的TextInput组件:

        <TextInput
          placeholder="Email ID"
          onChangeText={(text) => this.validate(text)}
          value={this.state.email}
        />
    
    Run Code Online (Sandbox Code Playgroud)

  • 请注意,正则表达式仅匹配传统的顶级域名,例如“.com”或“.de”,而不匹配“.berlin”或“.shop”等(新)域名。末尾的“.\w{2,3}”需要类似于“.\w{2,}”。有关更复杂的正则表达式,请参阅 https://emailregex.com/ (3认同)