javascript - Simple function to Validate checkboxes in reactJS - Stack Overflow

I have made a selection limit function which ensures that total checkbox is above min value and under m

I have made a selection limit function which ensures that total checkbox is above min value and under max value, these values were taken from the JSON which the checkboxes are mapped from, now the selection limit works, but I am trying to add validation to show warnings using onblur, but I am not sure how can the same function can be translated into an onblur validation function. for example where if someone unchecks , it shows on the console that you need to select a minimum of 3 until 3 is checked, same logic as selectData().

Function

  selectData(id, event) {
    let isSelected = event.currentTarget.checked;
    if (isSelected) {
      if (this.state.currentData < this.props.max) {
        this.setState({ currentData: this.state.currentData + 1 });
      } else {
        event.preventDefault();
        event.currentTarget.checked = false;
      }
    } else {
      if (this.state.currentData >= this.props.min) {
        this.setState({ currentData: this.state.currentData - 1 });
      } else {
        event.preventDefault();
        event.currentTarget.checked = true;
      }
    }
  }

Full Code:

I have made a selection limit function which ensures that total checkbox is above min value and under max value, these values were taken from the JSON which the checkboxes are mapped from, now the selection limit works, but I am trying to add validation to show warnings using onblur, but I am not sure how can the same function can be translated into an onblur validation function. for example where if someone unchecks , it shows on the console that you need to select a minimum of 3 until 3 is checked, same logic as selectData().

Function

  selectData(id, event) {
    let isSelected = event.currentTarget.checked;
    if (isSelected) {
      if (this.state.currentData < this.props.max) {
        this.setState({ currentData: this.state.currentData + 1 });
      } else {
        event.preventDefault();
        event.currentTarget.checked = false;
      }
    } else {
      if (this.state.currentData >= this.props.min) {
        this.setState({ currentData: this.state.currentData - 1 });
      } else {
        event.preventDefault();
        event.currentTarget.checked = true;
      }
    }
  }

Full Code: https://codesandbox.io/embed/48o2jo2500?fontsize=14

Share Improve this question edited Mar 15, 2019 at 23:42 AlxL asked Mar 13, 2019 at 21:59 AlxLAlxL 1391 silver badge8 bronze badges 5
  • How this onblur event will be caleed? – Maheer Ali Commented Mar 16, 2019 at 3:24
  • from the input form like <input onblur={function}.... – AlxL Commented Mar 16, 2019 at 7:10
  • Can you please give example in code.pen. By ment the line and adding the event which would only console.log('x') Then i can try to add functionality you want – Maheer Ali Commented Mar 16, 2019 at 9:08
  • @AlxL I'm sorry, but the full code link does not work. Could you please fix it so that I can see the full ponent source? – Tomasz Kajtoch Commented Mar 20, 2019 at 12:49
  • codesandbox.io/embed/8212q2pmw0?fontsize=14 – AlxL Commented Mar 21, 2019 at 14:56
Add a ment  | 

1 Answer 1

Reset to default 4 +50

Your general approach seems like it should work, just need the finesse for actually implementing the error states. A suggestion here though would be to update your !isSelected && this.state.currentData < this.props.min condition to allow for less than three to be selected but to display an error state to the user.

  ...
    } else {
      if (this.state.currentData >= this.props.min) {
        // this.setState({ currentData: this.state.currentData - 1 });
        // UPDATE:
        this.setState((state) => ({ currentData: state.currentData - 1 }));
      } else {
        event.preventDefault();
        // Don't force the box to be selected, show error state instead
        // Disable calls to filtering, sorting, etc. until error resolved
        // event.currentTarget.checked = true;
      }
    }
  }

Basic Implementation:

  • Vanilla JavaScript: https://codepen.io/hollyos/pen/XGqryV
  • React: https://codepen.io/hollyos/pen/EMpXdR

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744964323a4603580.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信