content logo

React Forms:

Javascript Email Validation Form using React

Subscribing to the website newsletter usually requires a form to enter the email. In this post, we have prepared this form for you. In this form, by entering the email incorrectly, a message titled Please enter a valid email is written below the form. Around the form is a red border. By entering the correct email, in addition to deleting this board, the phrase below the form will also be deleted. This form was created using React and is suitable for dark theme websites.

#

React Email Validation Form Code

#

HTML Form Validating by React

#

React JS Email Form

#

Javascript Dynamic Email Validation

<!-- This script got from frontendfreecode.com -->
<div id="app">
</div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
html, body {
  height: 100%;
}
body {
  background: #333;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: Helvetica Neue;
}
h1 {
  font-size: 2em;
  color: #eee;
  display: inline-block;
}
a {
  color: white;
}
p {
  margin-top: 1em;
  text-align: center;
  color: #aaa;
}
input {
  background: white;
  border-radius: 4px;
  border: 2px solid #e8e8e8;
  width: 95%;
  height: 38px;
  margin-top: 5px;
  padding-left: 10px;
  padding-right: 10px;
  box-sizing: border-box;
}
.field-container {
  text-align: left;
}
.field-container span {
  display: none;
}
.field-container.error:before {
  content: "x";
  display: inline-block;
  float: right;
  margin-top: 10px;
  font-weight: 600;
  color: #f0a2a1;
}
.field-container.error span {
  display: inline-block;
  font-size: 16px;
  text-align: left;
  width: 100%;
  font-style: italic;
  color: #f0a2a1;
}
.field-container.error input {
  border: 2px solid #f0a2a1;
}
class Application extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            email: '',
            valid: true
        };
        this.handleEmailChange = this.handleEmailChange.bind(this);
    }
    validateEmail(email) {
        const 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,}))$/;
        return re.test(email);
    }
    handleEmailChange(e) {
        const email = e.target.value;
        const emailValid = this.validateEmail(email);
        this.setState({
            email: e.target.value,
            valid: emailValid
        });
    }
    render() {
        let fieldContainerClass = 'field-container';
        const { email, valid } = this.state;
        if (!valid) {
            fieldContainerClass += ' error';
        }
        return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/
            React.createElement("h1", null, "Simple React form validation"), /*#__PURE__*/
            React.createElement("div", { className: fieldContainerClass }, /*#__PURE__*/
                React.createElement("input", { type: "email", value: this.state.email, onChange: this.handleEmailChange, placeholder: "Email" }), /*#__PURE__*/
                React.createElement("span", null, "Invalid e-mail address")));
    }
}
ReactDOM.render( /*#__PURE__*/React.createElement(Application, null), document.getElementById('app'));
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.3.1/react-dom.min.js'></script>
<style>
html, body {
  height: 100%;
}
body {
  background: #333;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: Helvetica Neue;
}
h1 {
  font-size: 2em;
  color: #eee;
  display: inline-block;
}
a {
  color: white;
}
p {
  margin-top: 1em;
  text-align: center;
  color: #aaa;
}
input {
  background: white;
  border-radius: 4px;
  border: 2px solid #e8e8e8;
  width: 95%;
  height: 38px;
  margin-top: 5px;
  padding-left: 10px;
  padding-right: 10px;
  box-sizing: border-box;
}
.field-container {
  text-align: left;
}
.field-container span {
  display: none;
}
.field-container.error:before {
  content: "x";
  display: inline-block;
  float: right;
  margin-top: 10px;
  font-weight: 600;
  color: #f0a2a1;
}
.field-container.error span {
  display: inline-block;
  font-size: 16px;
  text-align: left;
  width: 100%;
  font-style: italic;
  color: #f0a2a1;
}
.field-container.error input {
  border: 2px solid #f0a2a1;
}
</style>

</head>
<body>
<div id="app">
</div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
class Application extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            email: '',
            valid: true
        };
        this.handleEmailChange = this.handleEmailChange.bind(this);
    }
    validateEmail(email) {
        const 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,}))$/;
        return re.test(email);
    }
    handleEmailChange(e) {
        const email = e.target.value;
        const emailValid = this.validateEmail(email);
        this.setState({
            email: e.target.value,
            valid: emailValid
        });
    }
    render() {
        let fieldContainerClass = 'field-container';
        const { email, valid } = this.state;
        if (!valid) {
            fieldContainerClass += ' error';
        }
        return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/
            React.createElement("h1", null, "Simple React form validation"), /*#__PURE__*/
            React.createElement("div", { className: fieldContainerClass }, /*#__PURE__*/
                React.createElement("input", { type: "email", value: this.state.email, onChange: this.handleEmailChange, placeholder: "Email" }), /*#__PURE__*/
                React.createElement("span", null, "Invalid e-mail address")));
    }
}
ReactDOM.render( /*#__PURE__*/React.createElement(Application, null), document.getElementById('app'));
</script>

</body>
</html>
Preview