1

Here I provide my code where I want to enter characters into the password input field and I want to do not enter whitespace/space in it but it also going inside of it instead when I print input value then it does not contain space/whitespace. Please help me to resolve the issue.

CodeSandBox Demo

Code -

import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
import { Form, Input, Label } from "semantic-ui-react";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      inputFieldData: {
        pass: {
          val: "",
          error: false
        }
      }
    };
  }

  inputChange = e => {
    // prevent not to enter space charactes in password field while registering
    const re = /^\S*$/;
    let inputFieldData = this.state.inputFieldData;
    const name = e.target.name;
    if (re.test(e.target.value)) {
      inputFieldData[name]["val"] = e.target.value;
      console.log(e.target.value);
    }
    this.setState({ inputFieldData });
  };

  render() {
    const { inputFieldData } = this.state;

    return (
      <div className="App">
        <h1>Input box does not contain space in it</h1>
        <h3>Input Value: {inputFieldData["pass"]["val"]}</h3>
        <Form className="register-form">
          <Form.Field>
            <Input
              type="password"
              icon="user circle"
              name="pass"
              iconPosition="left"
              placeholder="Enter Password"
              onChange={this.inputChange}
              error={inputFieldData["pass"]["error"]}
            />
            {inputFieldData["pass"]["error"] && (
              <Label basic color="red" pointing>
                Field can not be empty
              </Label>
            )}
          </Form.Field>
        </Form>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Shubham Baranwal
  • 2,492
  • 3
  • 14
  • 26

4 Answers4

2

You need to use the controlled component pattern (see here https://reactjs.org/docs/forms.html#controlled-components) by adding the value prop to your input component.

You can then add the space trimming logic in your handleChange function.

Here is an example : https://codesandbox.io/s/kkpr53k36r

1

You need to set the value of the input to inputFieldData.pass.val,
otherwise it is not bound to the state of your component.

        <Input
          value={inputFieldData.pass.val}
          type="password"
          icon="user circle"
          name="pass"
          iconPosition="left"
          placeholder="Enter Password"
          onChange={this.inputChange}
          error={inputFieldData["pass"]["error"]}
        />
maioman
  • 18,154
  • 4
  • 36
  • 42
1

I solved this problem with regex

str.replace(/\s/g, '')
HuyNT
  • 11
  • 1
  • 1
  • It works partially, but he also have to set the value to the input fields as without value component works as uncontrolled one and it will have its own state. – Tmh Jul 19 '21 at 06:26
-1

Simply do like this in handleOnChange Function:

 handleOnChange = (e) => {
    this.setState({
      [e.target.name]: e.target.value.split(" ").join("")
    });
  };