Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
edited tags
Link
deleted 7 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Validating input in a reactReact app

I was writing some validation logic for a reactReact-native app and thought iI had so many bools flying around in my code!

Is there any way to reduce redundancy in this code? or is this the best that can be done.?

_validatePhone() {
    if (!validate.validatePhone(this.state.phone)) {
      this.setState({ phoneError: true });
      return false;
    }
  return true;
}

_validateName() {
    if (!validate.validateName(this.state.name)) {
      this.setState({ nameError: true });
      return false;
    }
    return true;
  }

  _validateForm() {
    this._resetErrors(); // Reset errors before validating
    if (!this._validateName()) {
      return false;
    } else if (!this._validatePhone()) {
      return false;
    }
    return true;
  }

Validating input in a react app

I was writing some validation logic for a react-native app and thought i had so many bools flying around in my code!

Is there any way to reduce redundancy in this code? or is this the best that can be done.

_validatePhone() {
    if (!validate.validatePhone(this.state.phone)) {
      this.setState({ phoneError: true });
      return false;
    }
  return true;
}

_validateName() {
    if (!validate.validateName(this.state.name)) {
      this.setState({ nameError: true });
      return false;
    }
    return true;
  }

  _validateForm() {
    this._resetErrors(); // Reset errors before validating
    if (!this._validateName()) {
      return false;
    } else if (!this._validatePhone()) {
      return false;
    }
    return true;
  }

Validating input in a React app

I was writing some validation logic for a React-native app and thought I had so many bools flying around in my code!

Is there any way to reduce redundancy in this code or is this the best that can be done?

_validatePhone() {
    if (!validate.validatePhone(this.state.phone)) {
      this.setState({ phoneError: true });
      return false;
    }
  return true;
}

_validateName() {
    if (!validate.validateName(this.state.name)) {
      this.setState({ nameError: true });
      return false;
    }
    return true;
  }

  _validateForm() {
    this._resetErrors(); // Reset errors before validating
    if (!this._validateName()) {
      return false;
    } else if (!this._validatePhone()) {
      return false;
    }
    return true;
  }
edited title
Link
Loading
Source Link
Loading