JavaScript How to process onChange and change focus to new <Field>?

I'm using Formik (with withFormik()) and want to check a <Field> as a user types in it - after it has 4 characters in it, I want to focus on the next field so they can keep typing without having to move to the next field.

So my InnerForm has:

<Field
  type="text"
  name="credit1"
  inputmode="numeric"
  maxlength="4" />
<Field
  type="text"
  name="credit2"
  inputmode="numeric"
  maxlength="4" />

And my FormikInnerFormContainer = withFormik(...) has a validationSchema.

How could I catch changes on the first field, and move focus to the 2nd field if the first has 4 characters in?

I tried to override the onChange, but couldn't figure out how to update the Field contents with each character the user types.

Answer:1

You might use like this in Formik.

  focusChange(e) {
    if (e.target.value.length >= e.target.getAttribute("maxlength")) {
      e.target.nextElementSibling.focus();
    }
    ...

//Example implementation
import React from "react";
import { Formik } from "formik";

export default class Basic extends React.Component {
  constructor(props) {
    super(props);
    this.inputRef = React.createRef();
    this.focusChange = this.focusChange.bind(this);
  }

  focusChange(e) {
    if (e.target.value.length >= e.target.getAttribute("maxlength")) {
      e.target.nextElementSibling.focus();
    }
  }

  render() {
    return (
      <div>
        <h1>My Form</h1>
        <Formik
          initialValues={{ name: "" }}
          onSubmit={(values, actions) => {
            setTimeout(() => {
              alert(JSON.stringify(values, null, 2));
              actions.setSubmitting(false);
            }, 1000);
          }}
          render={props => (
            <form onSubmit={props.handleSubmit} ref={this.inputRef}>
              <input
                type="text"
                onChange={props.handleChange}
                onBlur={props.handleBlur}
                value={props.values.name}
                name="name"
                maxlength="4"
                onInput={e => this.focusChange(e)}
              />
              <input
                type="text"
                onChange={props.handleChange}
                onBlur={props.handleBlur}
                value={props.values.lastName}
                name="lastName"
                maxlength="4"
                onInput={this.focusChange}
              />
              <button type="submit">Submit</button>
            </form>
          )}
        />
      </div>
    );
  }
}
Answer:2

I have a form with: onSubmit="return myFunction()" And in my script: function myFunction(){ alert("hello world"); return false; } I have checked in Crhome and Firefox, everything works perfect,...

I have a form with: onSubmit="return myFunction()" And in my script: function myFunction(){ alert("hello world"); return false; } I have checked in Crhome and Firefox, everything works perfect,...

  1. onsubmit return false doesn't work
  2. onsubmit return false

I am trying to make a function that takes a string and a delimiter and splits the string it into an array, while keeping the delimiter, using case insensitivity for the search, and preserving original ...

I am trying to make a function that takes a string and a delimiter and splits the string it into an array, while keeping the delimiter, using case insensitivity for the search, and preserving original ...

  1. js split string
  2. js split string by comma
  3. js split string to array
  4. js split string into array
  5. js split string by character
  6. js split string by space
  7. js split string at index
  8. js split string into chunks
  9. js split string by newline
  10. js split string every n characters
  11. js split string by length
  12. js split string by regex
  13. js split string by whitespace
  14. js split string into characters
  15. js split string by delimiter
  16. js split string and get last element
  17. js split string in half
  18. js split string by multiple characters
  19. js split string by capital letters
  20. js split string into array by comma

<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); transition: border 0.4s, box-shadow 0.4s; background-color: ...

<span class="switchery switchery-small" style="box-shadow: rgb(223, 223, 223) 0px 0px 0px 0px inset; border-color: rgb(223, 223, 223); transition: border 0.4s, box-shadow 0.4s; background-color: ...

  1. event binding for elements

I'm using the body-parser package like this: // For parsing application/json: app.use(require('body-parser').json()); // For parsing application/x-www-form-urlencoded app.use(require('body-parser')....

I'm using the body-parser package like this: // For parsing application/json: app.use(require('body-parser').json()); // For parsing application/x-www-form-urlencoded app.use(require('body-parser')....