content logo

React Forms:

Ajax Form Example with Validation using React

In this post, as you can see, we have prepared a form. This form uses React. You can use this form to contact us on your website. This form has a white background and the message button is yellow. Each text field has a gray line below it. By clicking on any text field, this gray line will turn yellow.

#

React Form Code

#

React Ajax Form

#

HTML Contact Us Form

#

HTML Responsive Form

#

React Form Validation

<!-- This script got from frontendfreecode.com -->
<div class="react-form-container"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
html {
  box-sizing: border-box;
  font-size: 16px;
}
*,
*:after,
*:before {
  box-sizing: border-box;
}
body {
  font: 100% "Roboto", arial, sans-serif;
  background: #f5f5f5;
}
form {
  padding: 2rem;
  margin-top: 2rem;
  margin-right: auto;
  margin-left: auto;
  max-width: 23.75rem;
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
}
h1 {
  margin-top: 0;
  margin-bottom: 3.236rem;
  text-align: center;
  font-size: 1.618rem;
}
.form-group {
  padding: 0;
  border: 0;
}
.form-group + .form-group {
  margin-top: 1rem;
}
label {
  display: inline-block;
  margin-bottom: 0.5rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  touch-action: manipulation;
}
input,
textarea {
  display: block;
  padding: 0.5rem 0.75rem;
  width: 100%;
  font-size: 1rem;
  line-height: 1.25;
  color: #55595c;
  background-color: #fff;
  background-image: none;
  background-clip: padding-box;
  border-top: 0;
  border-right: 0;
  border-bottom: 1px solid #eee;
  border-left: 0;
  border-radius: 3px;
  transition: all 0.25s cubic-bezier(0.4, 0, 1, 1);
}
input:focus,
textarea:focus {
  outline: 0;
  border-bottom-color: #ffab00;
}
textarea {
  resize: vertical;
}
.btn {
  display: inline-block;
  padding: 0.75rem 1rem;
  margin-top: 1.618rem;
  font-weight: 400;
  text-align: center;
  text-transform: uppercase;
  color: #fff;
  vertical-align: middle;
  white-space: nowrap;
  background-color: #ffab00;
  border: 1px solid transparent;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
  cursor: pointer;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
  transition: all 0.25s cubic-bezier(0.4, 0, 1, 1);
}
.btn:focus, .btn:hover {
  background-color: #ffc142;
  box-shadow: 0 18px 35px rgba(50, 50, 93, 0.1), 0 8px 15px rgba(0, 0, 0, 0.07);
}
.btn:focus {
  outline: 0;
}
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const reactFormContainer = document.querySelector('.react-form-container');
class ReactFormLabel extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return /*#__PURE__*/(
            React.createElement("label", { htmlFor: this.props.htmlFor }, this.props.title));
    }
}
class ReactForm extends React.Component {
    constructor(props) {
        super(props); _defineProperty(this, "handleChange",
            e => {
                let newState = {};
                newState[e.target.name] = e.target.value;
                this.setState(newState);
            }); _defineProperty(this, "handleSubmit",
                (e, message) => {
                    e.preventDefault();
                    let formData = {
                        formSender: this.state.name,
                        formEmail: this.state.email,
                        formSubject: this.state.subject,
                        formMessage: this.state.message
                    };
                    if (formData.formSender.length < 1 || formData.formEmail.length < 1 || formData.formSubject.length < 1 || formData.formMessage.length < 1) {
                        return false;
                    }
                    $.ajax({
                        url: '/some/url',
                        dataType: 'json',
                        type: 'POST',
                        data: formData,
                        success: function (data) {
                            if (confirm('Thank you for your message. Can I erase the form?')) {
                                this.setState({
                                    firstName: '',
                                    lastName: '',
                                    email: '',
                                    subject: '',
                                    message: ''
                                });
                            }
                        },
                        error: function (xhr, status, err) {
                            console.error(status, err.toString());
                            alert('There was some problem with sending your message.');
                        }
                    });
                    this.setState({
                        firstName: '',
                        lastName: '',
                        email: '',
                        subject: '',
                        message: ''
                    });
                }); this.state = { name: '', email: '', subject: '', message: '' }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
    }
    render() {
        return /*#__PURE__*/(
            React.createElement("form", { className: "react-form", onSubmit: this.handleSubmit }, /*#__PURE__*/
                React.createElement("h1", null, "Say Hi!"), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formName", title: "Full Name:" }), /*#__PURE__*/
                    React.createElement("input", { id: "formName", className: "form-input", name: "name", type: "text", required: true, onChange: this.handleChange, value: this.state.name })), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formEmail", title: "Email:" }), /*#__PURE__*/
                    React.createElement("input", { id: "formEmail", className: "form-input", name: "email", type: "email", required: true, onChange: this.handleChange, value: this.state.email })), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formSubject", title: "Subject:" }), /*#__PURE__*/
                    React.createElement("input", { id: "formSubject", className: "form-input", name: "subject", type: "text", required: true, onChange: this.handleChange, value: this.state.subject })), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formMessage", title: "Message:" }), /*#__PURE__*/
                    React.createElement("textarea", { id: "formMessage", className: "form-textarea", name: "message", required: true, onChange: this.handleChange })), /*#__PURE__*/
                React.createElement("div", { className: "form-group" }, /*#__PURE__*/
                    React.createElement("input", { id: "formButton", className: "btn", type: "submit", placeholder: "Send message" }))));
    }
}
ReactDOM.render( /*#__PURE__*/React.createElement(ReactForm, null), reactFormContainer);
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css'>
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react-dom.min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css'>
<link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react-dom.min.js'></script>
<style>
html {
  box-sizing: border-box;
  font-size: 16px;
}
*,
*:after,
*:before {
  box-sizing: border-box;
}
body {
  font: 100% "Roboto", arial, sans-serif;
  background: #f5f5f5;
}
form {
  padding: 2rem;
  margin-top: 2rem;
  margin-right: auto;
  margin-left: auto;
  max-width: 23.75rem;
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
}
h1 {
  margin-top: 0;
  margin-bottom: 3.236rem;
  text-align: center;
  font-size: 1.618rem;
}
.form-group {
  padding: 0;
  border: 0;
}
.form-group + .form-group {
  margin-top: 1rem;
}
label {
  display: inline-block;
  margin-bottom: 0.5rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  touch-action: manipulation;
}
input,
textarea {
  display: block;
  padding: 0.5rem 0.75rem;
  width: 100%;
  font-size: 1rem;
  line-height: 1.25;
  color: #55595c;
  background-color: #fff;
  background-image: none;
  background-clip: padding-box;
  border-top: 0;
  border-right: 0;
  border-bottom: 1px solid #eee;
  border-left: 0;
  border-radius: 3px;
  transition: all 0.25s cubic-bezier(0.4, 0, 1, 1);
}
input:focus,
textarea:focus {
  outline: 0;
  border-bottom-color: #ffab00;
}
textarea {
  resize: vertical;
}
.btn {
  display: inline-block;
  padding: 0.75rem 1rem;
  margin-top: 1.618rem;
  font-weight: 400;
  text-align: center;
  text-transform: uppercase;
  color: #fff;
  vertical-align: middle;
  white-space: nowrap;
  background-color: #ffab00;
  border: 1px solid transparent;
  box-shadow: 0 15px 35px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
  cursor: pointer;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
  transition: all 0.25s cubic-bezier(0.4, 0, 1, 1);
}
.btn:focus, .btn:hover {
  background-color: #ffc142;
  box-shadow: 0 18px 35px rgba(50, 50, 93, 0.1), 0 8px 15px rgba(0, 0, 0, 0.07);
}
.btn:focus {
  outline: 0;
}
</style>

</head>
<body>
<div class="react-form-container"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } const reactFormContainer = document.querySelector('.react-form-container');
class ReactFormLabel extends React.Component {
    constructor(props) {
        super(props);
    }
    render() {
        return /*#__PURE__*/(
            React.createElement("label", { htmlFor: this.props.htmlFor }, this.props.title));
    }
}
class ReactForm extends React.Component {
    constructor(props) {
        super(props); _defineProperty(this, "handleChange",
            e => {
                let newState = {};
                newState[e.target.name] = e.target.value;
                this.setState(newState);
            }); _defineProperty(this, "handleSubmit",
                (e, message) => {
                    e.preventDefault();
                    let formData = {
                        formSender: this.state.name,
                        formEmail: this.state.email,
                        formSubject: this.state.subject,
                        formMessage: this.state.message
                    };
                    if (formData.formSender.length < 1 || formData.formEmail.length < 1 || formData.formSubject.length < 1 || formData.formMessage.length < 1) {
                        return false;
                    }
                    $.ajax({
                        url: '/some/url',
                        dataType: 'json',
                        type: 'POST',
                        data: formData,
                        success: function (data) {
                            if (confirm('Thank you for your message. Can I erase the form?')) {
                                this.setState({
                                    firstName: '',
                                    lastName: '',
                                    email: '',
                                    subject: '',
                                    message: ''
                                });
                            }
                        },
                        error: function (xhr, status, err) {
                            console.error(status, err.toString());
                            alert('There was some problem with sending your message.');
                        }
                    });
                    this.setState({
                        firstName: '',
                        lastName: '',
                        email: '',
                        subject: '',
                        message: ''
                    });
                }); this.state = { name: '', email: '', subject: '', message: '' }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
    }
    render() {
        return /*#__PURE__*/(
            React.createElement("form", { className: "react-form", onSubmit: this.handleSubmit }, /*#__PURE__*/
                React.createElement("h1", null, "Say Hi!"), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formName", title: "Full Name:" }), /*#__PURE__*/
                    React.createElement("input", { id: "formName", className: "form-input", name: "name", type: "text", required: true, onChange: this.handleChange, value: this.state.name })), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formEmail", title: "Email:" }), /*#__PURE__*/
                    React.createElement("input", { id: "formEmail", className: "form-input", name: "email", type: "email", required: true, onChange: this.handleChange, value: this.state.email })), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formSubject", title: "Subject:" }), /*#__PURE__*/
                    React.createElement("input", { id: "formSubject", className: "form-input", name: "subject", type: "text", required: true, onChange: this.handleChange, value: this.state.subject })), /*#__PURE__*/
                React.createElement("fieldset", { className: "form-group" }, /*#__PURE__*/
                    React.createElement(ReactFormLabel, { htmlFor: "formMessage", title: "Message:" }), /*#__PURE__*/
                    React.createElement("textarea", { id: "formMessage", className: "form-textarea", name: "message", required: true, onChange: this.handleChange })), /*#__PURE__*/
                React.createElement("div", { className: "form-group" }, /*#__PURE__*/
                    React.createElement("input", { id: "formButton", className: "btn", type: "submit", placeholder: "Send message" }))));
    }
}
ReactDOM.render( /*#__PURE__*/React.createElement(ReactForm, null), reactFormContainer);
</script>

</body>
</html>
Preview