Email Validation (React Native). Returning the result as 'invalid' for all the entries
Ok I got the code working, below you can take the look for validating the email on each user input :
- 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");
}
}
- You TextInput Component:
<TextInput
placeholder="Email ID"
onChangeText={(text) => this.validate(text)}
value={this.state.email}
/>
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 });
}
}