Everybody made some valid points here, And you will have to follow almost all of these to validate the data.
Anyways, As of now i think this would be better way to move forward with email validation.
- Set
<input type="email" /> so browsers can validate the email for you
- Validate it using javascript
- Perform validation on server
You can render the input like
<input type="email" value={ this.state.email } onChange={ handleOnChange } />
And your handleOnChange function code will look something like this
let handleOnChange = ( email ) => {
// don't remember from where i copied this code, but this works.
let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if ( re.test(email) ) {
// this is a valid email address
// call setState({email: email}) to update the email
// or update the data in redux store.
}
else {
// invalid email, maybe show an error to the user.
}
}
NOTE : Depending on how you've setup everything you might have to use this.handleOnChange instead of handleOnChange and also bind it in constructor like this.handleOnChange = this.handleOnChange.bind(this);
...
And when you make post request to the server/api with the email address, Validate the email on the server and if valid store it in database otherwise respond back with error.
You can just google node* validation libraries and use them to handle all the validations, you could use the email validation code from here... or validate it however you want.