Reactjs Form Validation Example with Demo

Reactjs Form Validation Example with Demo

Today, We want to share with you Reactjs Form Validation Example with Demo.In this post we will show you Reactjs – Form input validation, hear for Up and Running with React Form Validation we will give you demo and example for implement.In this post, we will learn about How to do Simple Form Validation in Reactjs with an example.

Reactjs Form Validation Example with Demo

There are the Following The simple About Reactjs Form Validation Example with Demo Full Information With Example and source code.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

As I will cover this Post with live Working example to develop react form validation best practices, so the react form validation npm for this example is following below.

Step 1: Reactjs Part

React + No-Library (pure JS)

class Product extends React.Component {
  constructor(props){
    super(props);

    this.state = {
      fields: {},
      errors: {}
    }
  }

  handleValidation(){
    let fields = this.state.fields;
    let errors = {};
    let formIsValid = true;

    //here User Name
    if(!fields["name"]){
      formIsValid = false;
      errors["name"] = "Sorry, To have Cannot be empty";
    }

    if(typeof fields["name"] !== "undefined"){
      if(!fields["name"].match(/^[a-zA-Z]+$/)){
        formIsValid = false;
        errors["name"] = "Only letters";
      }      	
    }

    //check User Email
    if(!fields["email"]){
      formIsValid = false;
      errors["email"] = "Cannot be empty";
    }

    if(typeof fields["email"] !== "undefined"){
      let lastAtPos = fields["email"].lastIndexOf('@');
      let lastDotPos = fields["email"].lastIndexOf('.');

      if (!(lastAtPos  0 && fields["email"].indexOf('@@') == -1 && lastDotPos > 2 && (fields["email"].length - lastDotPos) > 2)) {
        formIsValid = false;
        errors["email"] = "Email is not valid";
      }
    }
    this.setState({errors: errors});
    return formIsValid;
  }

  contactSubmit(e){
    e.preventDefault();
    if(this.handleValidation()){
      alert("Form submitted");
    }else{
      alert("Form has errors.")
    }

  }

  handleChange(field, e){    		
    let fields = this.state.fields;
    fields[field] = e.target.value;        
    this.setState({fields});
  }

  render(){
    return (
      <div>        	
        <form name="fillContactUsFrm">
          <div>
            <fieldset>
              
              <span>{this.state.errors["name"]}</span>
              <br />
              
              <span>{this.state.errors["email"]}</span>
              <br />
              
              <br />
              
              <br />
            </fieldset>
          </div>
          <div>
            <fieldset>
              <textarea cols="28" rows="10">{this.state.fields["comment"]}</textarea>
            </fieldset>
          </div>
          <div>
            <fieldset>
              <button id="submit" value="Submit">Send Comment</button>
            </fieldset>
          </div>
        </form>
      </div>
    )
  }
}

ReactDOM.render(, document.querySelector("#root"))

Step 2: HTML Part

	<div id="root"></div>

Step 3: CSS Part

style.css

.fillContactUsFrm{
  margin: 20px;
}

input{
  margin-bottom: 5px;
  height: 20px;
  border: 1px solid #d8d8d8;
  padding: 3px;
}

textarea{
  border: 1px solid #d8d8d8;
  padding: 3px;
}

button{
  padding: 10px;
  border: 1px solid #d8d8d8;
  background-color: #f9f9f9;
  border-radius: 3px;
}

.error{
  margin-left: 5px;
  font-size: 13px;
  color: red;
}

Reactjs – Form input validation

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Reactjs Form Validation Example with Demo.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Form Validation Using React Examples
  2.   Get last inserted ID laravel 5.7 Examples
  3.   Handle CORS Requests in Laravel 6 Application
  4.   Dynamically Add Input Fields And Submit To Database With jQuery and PHP
  5.   How to Open URL in New Tab using Drupal
  6.   Accessing Complex Nested JSON objects in VueJs
  7.   Laravel AJAX Delete Record From MySQL Table
  8.   Vue.js Multi Step Form with Progress Bar
  9.   jQuery Disable enable Selector Examples
  10.   How to convert a String to Char Array in Java?