Skip to main content
Tweeted twitter.com/StackCodeReview/status/1342485275801497602
edited tags
Link
added 86 characters in body; edited title
Source Link
saran3h
  • 255
  • 1
  • 5

Validating anjavascript onChange event in javascript

I'm looking to simplify the below code for validating an onChange event. The allowedInput is a regex that is passed (optional) from all the form components.

    const validatedOnChange = (event: ChangeEvent<HTMLInputElement>): void => {
        if (isDisabled) {
            return;
        }
        if (allowedInput) {
            if (allowedInput.test(event.target.value)) {
                onChange({ name, value: event.target.value });
            }
            return;
        }
        onChange({ name, value: event.target.value });
    };

This is a common utility method used for all my React form components. Hence looking to write a readable and consistent code.

Validating an onChange event in javascript

I'm looking to simplify the below code for validating an onChange event.

    const validatedOnChange = (event: ChangeEvent<HTMLInputElement>): void => {
        if (isDisabled) {
            return;
        }
        if (allowedInput) {
            if (allowedInput.test(event.target.value)) {
                onChange({ name, value: event.target.value });
            }
            return;
        }
        onChange({ name, value: event.target.value });
    };

This is a common utility method used for all my React form components. Hence looking to write a readable and consistent code.

Validating javascript onChange event

I'm looking to simplify the below code for validating an onChange event. The allowedInput is a regex that is passed (optional) from all the form components.

    const validatedOnChange = (event: ChangeEvent<HTMLInputElement>): void => {
        if (isDisabled) {
            return;
        }
        if (allowedInput) {
            if (allowedInput.test(event.target.value)) {
                onChange({ name, value: event.target.value });
            }
            return;
        }
        onChange({ name, value: event.target.value });
    };

This is a common utility method used for all my React form components. Hence looking to write a readable and consistent code.

Source Link
saran3h
  • 255
  • 1
  • 5

Validating an onChange event in javascript

I'm looking to simplify the below code for validating an onChange event.

    const validatedOnChange = (event: ChangeEvent<HTMLInputElement>): void => {
        if (isDisabled) {
            return;
        }
        if (allowedInput) {
            if (allowedInput.test(event.target.value)) {
                onChange({ name, value: event.target.value });
            }
            return;
        }
        onChange({ name, value: event.target.value });
    };

This is a common utility method used for all my React form components. Hence looking to write a readable and consistent code.