Waajdi Mk111
Waajdi Mk111

Reputation: 9

i want to use 2 function in onChange

import React, { Component } from "react"; import { Link } from   
    "react-router-dom";
        const emailRegx = /^(([^<>()\[\]\\.,;:\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,}))$/;
        export default class ChefInfo extends Component { constructor(props) { super(props);   this.state = {    eInput: "",  
    small: "" };

    - 

           ----------

           ---------
           }

         handleChange = e => {
           this.setState({
             eInput: e.target.value
           });
         };

         emailTest = () => {
           if (emailRegx.test(this.state.eInput) === false) {
             this.setState({
               small: "your email is inccorect"
             });
           } else {
             this.setState({
               small: ""
             });
           }
         };

         render() {
           return (
             <div className="big-parent">
               <form>
                 <div className="input">
                   <label>
                     <strong>E-mail</strong>
                   </label>
                   <input
                     type="email"
                     className="input-filed"
                     onChange={() => {    //here the problem
                       this.handleChange();
                       this.emailTest();
                     }}
                   />
                   <small className="small">{this.state.small}</small>
                 </div>
               </form>
               <a href="#" className="btn btn-dark button">
                 <strong>READY</strong>
               </a>
             </div>
           );
         }    }

Upvotes: 0

Views: 30

Answers (2)

wentjun
wentjun

Reputation: 42526

You can refactor you code such that it looks like this. This make your render/template code look cleaner.

handleEvent(event) {
  this.handleChange(event);
  this.emailTest();
}
.
.
.

<input
  type="email"
  className="input-filed"
  onChange={this.handleEvent}
/>

Upvotes: 1

bkm412
bkm412

Reputation: 1057

Your handlechange use event object parameter.

So you should pass event object.

onChange={(e) => {    //here the problem
    this.handleChange(e);
    this.emailTest();
}}

But in this case, you don't need to use two function.

This is enough.

handleChange = (e) => {
    this.setState({
        eInput : e.target.value,
        small : emailRegx.test(e.target.value) ? '' : "your email is incorrect"
    })
};

Upvotes: 1

Related Questions