content logo

React Forms:

Dynamic React Form with Text Validation and Submission Message

To register a user on the website, you can use the form we have provided for you in this post. This form has gray titles. Each field that is selected will be named blue. This form also has a blue registration button. The whole form and the whole button has an outer shadow. With the correct completion of the form and clicking on the form registration button, the phrase "Completed form" will be displayed along with a confirmation icon.

#

Animating React Form

#

React Form Effect

#

Responsive React Registeration Form

#

React Form Completion Message

<!-- 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>
                                                                            
body {
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
  background: #FEFEFE;
}
form {
  padding: 0em;
  box-shadow: 0px 3px 6px 1px #A8A8A8;
  border-radius: 10px;
  margin: 2em;
  padding: 2em;
}
.relative {
  position: relative;
  width: 400px;
  height: 30px;
  margin-bottom: 3em;
}
.relative.notValid {
  -webkit-animation: notValid 1s;
          animation: notValid 1s;
}
input, label {
  display: block;
  font-size: 1em;
  font-family: "Roboto", sans-serif;
  font-weight: bold;
  color: #777777;
}
input {
  width: 100%;
  height: 100%;
  border: none;
  border-bottom: 3px solid;
  border-color: #DFDFDF;
  transition: border-color 0.3s;
  background: transparent;
}
input:focus {
  border-color: #377AE6;
}
.error {
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
  -webkit-animation: error 1s;
          animation: error 1s;
}
input:focus ~ label, input:valid ~ label {
  top: -1em;
  font-size: 0.9em;
  color: #377AE6;
}
input[name=submit] {
  border: none;
  width: -webkit-fit-content;
  width: -moz-fit-content;
  width: fit-content;
  height: -webkit-fit-content;
  height: -moz-fit-content;
  height: fit-content;
  background: #377AE6;
  color: white;
  padding: 0.8em 2em;
  border-radius: 5px;
  margin: 0 auto;
  text-transform: uppercase;
  box-shadow: 0px 2px 10px -1px black;
}
label {
  position: absolute;
  top: 0;
  transition: all 0.3s;
}
@-webkit-keyframes error {
  10% {
    transform: translateX(0%);
    border-color: red;
  }
  20% {
    transform: translateX(-10%);
  }
  40% {
    transform: translateX(10%);
  }
  50% {
    transform: translateX(0%);
    background-color: rgba(255, 0, 0, 0.5);
  }
  60% {
    transform: translateX(-10%);
  }
  80% {
    transform: translateX(10%);
  }
  90% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(0%);
    border-color: red;
  }
}
@keyframes error {
  10% {
    transform: translateX(0%);
    border-color: red;
  }
  20% {
    transform: translateX(-10%);
  }
  40% {
    transform: translateX(10%);
  }
  50% {
    transform: translateX(0%);
    background-color: rgba(255, 0, 0, 0.5);
  }
  60% {
    transform: translateX(-10%);
  }
  80% {
    transform: translateX(10%);
  }
  90% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(0%);
    border-color: red;
  }
}
h1 {
  color: #777777;
  font-size: 3em;
  font-family: "Roboto", sans-serif;
  font-weight: bold;
}
.icon {
  width: 100%;
  text-align: center;
  color: #377AE6;
  font-size: 4em;
}
@media only screen and (max-width: 768px) {
  .relative {
    width: 60vw;
  }
}
class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {
        name: "",
        surname: "",
        email: "" } };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  handleChange(event) {
    let nextData = this.state.data;
    nextData[event.target.name] = event.target.value;
    this.setState({
      data: nextData });
  }
  handleSubmit(event)
  {
    event.preventDefault();
    let errors = 0;
    if (this.checkString(this.state.data.name, "name"))
    this.form.inputName.errorOff();else
    {
      this.form.inputName.errorOn();
      errors++;
    }
    if (this.checkString(this.state.data.surname, "surname"))
    this.form.inputSurname.errorOff();else
    {
      this.form.inputSurname.errorOn();
      errors++;
    }
    if (this.checkString(this.state.data.email, "email"))
    this.form.inputEmail.errorOff();else
    {
      this.form.inputEmail.errorOn();
      errors++;
    }
    if (errors == 0)
    this.form.formCompleted();
  }
  checkString(string, inputName) {
    switch (inputName) {
      case "name":
      case "surname":
        return string.match(/\w+/g);
      case "email":
        return string.match(/\w+@\w+/);}
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("div", null, /*#__PURE__*/
      React.createElement(Form, { ref: form => this.form = form, data: this.state.data, handleChange: this.handleChange, handleSubmit: this.handleSubmit })));
  }}
class Form extends React.Component {
  constructor(props)
  {
    super(props);
    this.state = {
      isFormComplete: false };
    this.formCompleted = this.formCompleted.bind(this);
  }
  formCompleted() {
    this.setState({
      isFormComplete: true });
  }
  render() {
    return (
      this.state.isFormComplete ? /*#__PURE__*/
      React.createElement("div", null, /*#__PURE__*/
      React.createElement("h1", null, "Form completed!"), /*#__PURE__*/
      React.createElement("i", { className: "fas fa-check-circle icon" })) : /*#__PURE__*/
      React.createElement("form", { onSubmit: this.props.handleSubmit }, /*#__PURE__*/
      React.createElement(TextInput, { ref: element => this.inputName = element, type: "text", name: "name", value: this.props.data.name, handleChange: this.props.handleChange }), /*#__PURE__*/
      React.createElement(TextInput, { ref: element => this.inputSurname = element, type: "text", name: "surname", value: this.props.data.surname, handleChange: this.props.handleChange }), /*#__PURE__*/
      React.createElement(TextInput, { ref: element => this.inputEmail = element, type: "text", name: "email", value: this.props.data.email, handleChange: this.props.handleChange }), /*#__PURE__*/
      React.createElement("input", { type: "submit", name: "submit" })));
  }}
class TextInput extends React.Component {
  constructor(props)
  {
    super(props);
    this.state = {
      isError: false };
  }
  errorOn() {
    this.setState({
      isError: true });
  }
  //
  errorOff() {
    this.setState({
      isError: false });
  }
  componentDidMount() {
    console.log(this.className);
  }
  componentDidUpdate() {
    console.log(this.element.className);
    //   this.element.className="relative";
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("div", { ref: element => this.element = element, className: this.state.isError ? "relative error" : "relative" }, /*#__PURE__*/
      React.createElement("input", { type: this.props.type, name: this.props.name, onChange: this.props.handleChange, value: this.props.value, required: true }), /*#__PURE__*/
      React.createElement("label", null, "Your ", this.props.name, " ")));
  }}
//render App component
ReactDOM.render( /*#__PURE__*/React.createElement(App, null), document.getElementById('app'));
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css" >
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.4.0/umd/react.development.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.0/umd/react-dom.development.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css" >
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.4.0/umd/react.development.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.0/umd/react-dom.development.js'></script>
<style>
body {
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
  background: #FEFEFE;
}
form {
  padding: 0em;
  box-shadow: 0px 3px 6px 1px #A8A8A8;
  border-radius: 10px;
  margin: 2em;
  padding: 2em;
}
.relative {
  position: relative;
  width: 400px;
  height: 30px;
  margin-bottom: 3em;
}
.relative.notValid {
  -webkit-animation: notValid 1s;
          animation: notValid 1s;
}
input, label {
  display: block;
  font-size: 1em;
  font-family: "Roboto", sans-serif;
  font-weight: bold;
  color: #777777;
}
input {
  width: 100%;
  height: 100%;
  border: none;
  border-bottom: 3px solid;
  border-color: #DFDFDF;
  transition: border-color 0.3s;
  background: transparent;
}
input:focus {
  border-color: #377AE6;
}
.error {
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
  -webkit-animation: error 1s;
          animation: error 1s;
}
input:focus ~ label, input:valid ~ label {
  top: -1em;
  font-size: 0.9em;
  color: #377AE6;
}
input[name=submit] {
  border: none;
  width: -webkit-fit-content;
  width: -moz-fit-content;
  width: fit-content;
  height: -webkit-fit-content;
  height: -moz-fit-content;
  height: fit-content;
  background: #377AE6;
  color: white;
  padding: 0.8em 2em;
  border-radius: 5px;
  margin: 0 auto;
  text-transform: uppercase;
  box-shadow: 0px 2px 10px -1px black;
}
label {
  position: absolute;
  top: 0;
  transition: all 0.3s;
}
@-webkit-keyframes error {
  10% {
    transform: translateX(0%);
    border-color: red;
  }
  20% {
    transform: translateX(-10%);
  }
  40% {
    transform: translateX(10%);
  }
  50% {
    transform: translateX(0%);
    background-color: rgba(255, 0, 0, 0.5);
  }
  60% {
    transform: translateX(-10%);
  }
  80% {
    transform: translateX(10%);
  }
  90% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(0%);
    border-color: red;
  }
}
@keyframes error {
  10% {
    transform: translateX(0%);
    border-color: red;
  }
  20% {
    transform: translateX(-10%);
  }
  40% {
    transform: translateX(10%);
  }
  50% {
    transform: translateX(0%);
    background-color: rgba(255, 0, 0, 0.5);
  }
  60% {
    transform: translateX(-10%);
  }
  80% {
    transform: translateX(10%);
  }
  90% {
    transform: translateX(0%);
  }
  100% {
    transform: translateX(0%);
    border-color: red;
  }
}
h1 {
  color: #777777;
  font-size: 3em;
  font-family: "Roboto", sans-serif;
  font-weight: bold;
}
.icon {
  width: 100%;
  text-align: center;
  color: #377AE6;
  font-size: 4em;
}
@media only screen and (max-width: 768px) {
  .relative {
    width: 60vw;
  }
}
</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 App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {
        name: "",
        surname: "",
        email: "" } };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  handleChange(event) {
    let nextData = this.state.data;
    nextData[event.target.name] = event.target.value;
    this.setState({
      data: nextData });
  }
  handleSubmit(event)
  {
    event.preventDefault();
    let errors = 0;
    if (this.checkString(this.state.data.name, "name"))
    this.form.inputName.errorOff();else
    {
      this.form.inputName.errorOn();
      errors++;
    }
    if (this.checkString(this.state.data.surname, "surname"))
    this.form.inputSurname.errorOff();else
    {
      this.form.inputSurname.errorOn();
      errors++;
    }
    if (this.checkString(this.state.data.email, "email"))
    this.form.inputEmail.errorOff();else
    {
      this.form.inputEmail.errorOn();
      errors++;
    }
    if (errors == 0)
    this.form.formCompleted();
  }
  checkString(string, inputName) {
    switch (inputName) {
      case "name":
      case "surname":
        return string.match(/\w+/g);
      case "email":
        return string.match(/\w+@\w+/);}
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("div", null, /*#__PURE__*/
      React.createElement(Form, { ref: form => this.form = form, data: this.state.data, handleChange: this.handleChange, handleSubmit: this.handleSubmit })));
  }}
class Form extends React.Component {
  constructor(props)
  {
    super(props);
    this.state = {
      isFormComplete: false };
    this.formCompleted = this.formCompleted.bind(this);
  }
  formCompleted() {
    this.setState({
      isFormComplete: true });
  }
  render() {
    return (
      this.state.isFormComplete ? /*#__PURE__*/
      React.createElement("div", null, /*#__PURE__*/
      React.createElement("h1", null, "Form completed!"), /*#__PURE__*/
      React.createElement("i", { className: "fas fa-check-circle icon" })) : /*#__PURE__*/
      React.createElement("form", { onSubmit: this.props.handleSubmit }, /*#__PURE__*/
      React.createElement(TextInput, { ref: element => this.inputName = element, type: "text", name: "name", value: this.props.data.name, handleChange: this.props.handleChange }), /*#__PURE__*/
      React.createElement(TextInput, { ref: element => this.inputSurname = element, type: "text", name: "surname", value: this.props.data.surname, handleChange: this.props.handleChange }), /*#__PURE__*/
      React.createElement(TextInput, { ref: element => this.inputEmail = element, type: "text", name: "email", value: this.props.data.email, handleChange: this.props.handleChange }), /*#__PURE__*/
      React.createElement("input", { type: "submit", name: "submit" })));
  }}
class TextInput extends React.Component {
  constructor(props)
  {
    super(props);
    this.state = {
      isError: false };
  }
  errorOn() {
    this.setState({
      isError: true });
  }
  //
  errorOff() {
    this.setState({
      isError: false });
  }
  componentDidMount() {
    console.log(this.className);
  }
  componentDidUpdate() {
    console.log(this.element.className);
    //   this.element.className="relative";
  }
  render() {
    return /*#__PURE__*/(
      React.createElement("div", { ref: element => this.element = element, className: this.state.isError ? "relative error" : "relative" }, /*#__PURE__*/
      React.createElement("input", { type: this.props.type, name: this.props.name, onChange: this.props.handleChange, value: this.props.value, required: true }), /*#__PURE__*/
      React.createElement("label", null, "Your ", this.props.name, " ")));
  }}
//render App component
ReactDOM.render( /*#__PURE__*/React.createElement(App, null), document.getElementById('app'));
</script>

</body>
</html>
Preview