react check if string is mail 1

react check if string is mail

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.
    }

}

Here is what the above code is Doing:
1. It’s checking if the email is valid or not.
2. If the email is valid, it’s updating the state.
3. If the email is invalid, it’s showing an error to the user.

Now, let’s see how we can do the same thing using a custom hook.

Similar Posts