Firebase 如何检查登记表中的文本输入是否为空?自然反应

Firebase 如何检查登记表中的文本输入是否为空?自然反应,firebase,react-native,redux,Firebase,React Native,Redux,我是一个新的本地开发人员。我使用React Native、Redux和Firebase制作了一份注册表,现在我想在用户注册之前检查所有字段是否都为空,以及用户是否使用了有效的电子邮件格式 代码如下: export class Register extends Component { constructor(props) { super(props); this.state = {

我是一个新的本地开发人员。我使用React Native、Redux和Firebase制作了一份注册表,现在我想在用户注册之前检查所有字段是否都为空,以及用户是否使用了有效的电子邮件格式 代码如下:

 export class Register extends Component {
         constructor(props) {
             super(props);
    
             this.state = { 
                 email: '',
                 password: '',
                 name: '',
                 lastname: ''
             }
    this.onSignUp = this.onSignUp.bind(this)
    
         }
    onSignUp(){
        const { email, password, name } = this.state;
        firebase.auth().createUserWithEmailAndPassword(email, password)
        .then((result) => {
            firebase.firestore().collection("Users")
                .doc(firebase.auth().currentUser.uid)
                .set({
                    name,
                    email
                })
            console.log(result)
        })
        .catch((error) => {
            console.log(error)
        })
    }
        render() {
            return (
               <View  style={{ flex: 1, justifyContent:'center' }}>
                    <TextInput
                   placeholder="name"
                   onChangeText={(name) => this.setState({ name })}
    
                   />
                   <TextInput
                   placeholder="email"
                   onChangeText={(email) => this.setState({ email })}
    
                   />
                   <TextInput
                   placeholder="password"
                   secureTextEntry={true}
                   onChangeText={(password) => this.setState({ password })}
    
                   />
                  <Button 
                  onPress={() => this.onSignUp()}
                  title= 'Sign Up'
                  />
               </View>
            )
        }
    }
    export default Register
导出类寄存器扩展组件{
建造师(道具){
超级(道具);
this.state={
电子邮件:“”,
密码:“”,
名称:“”,
姓氏:“”
}
this.onSignUp=this.onSignUp.bind(this)
}
onSignUp(){
const{email,password,name}=this.state;
firebase.auth().createUserWithEmailAndPassword(电子邮件,密码)
。然后((结果)=>{
firebase.firestore()集合(“用户”)
.doc(firebase.auth().currentUser.uid)
.设置({
名称
电子邮件
})
console.log(结果)
})
.catch((错误)=>{
console.log(错误)
})
}
render(){
返回(
this.setState({name})}
/>
this.setState({email})}
/>
this.setState({password})}
/>
this.onSignUp()}
标题='注册'
/>
)
}
}
导出默认寄存器
你可以像

if(name === '')
{
 Alert.alert("name is required")
}
else if(email === '')
{
 Alert.alert("email is required")
}
else if(password === '')
{
 Alert.alert("password is required")
}
else
{
 //call your API here
}

您可以使用基本的if-else语句,如下所示:

if (name === "") {
  console.log("Please Enter Name")
} else if (email === "") {
  console.log("Please Enter E-Mail") 
} else if (password === "") {
  console.log("Please Enter Password")
} else {
  // Sign Up
}
或者,您也可以使用正则表达式更精确:

if (/\w{4}/.test(name)) {
  console.log("Name must have at least 4 characters")
} else if (/\S+@\S+\.\S+/.test(email)) {
  console.log("Please enter a valid email")
} else if (/\w{8}/.test(password)) {
  console.log("Password must have at least 8 characters")
} else {
  //Sign up
}
你可以使用

if(name.trim() === ''){
  alert("name is required")
}

.trim()删除多余的空格,

我还将使用.trim()函数检查用户是否只输入了空格。是的,您可以使用.trim()来很好地工作,只需删除此.state.name之后的多余(&')谢谢你,我的错
if(name.trim() === ''){
  alert("name is required")
}