0

I need an input form that works with money. You can't type text, symbols, or anything besides numbers but you also shouldn't be able to put $0. It should be $1 and up. When I put /^[0-9\b]+$/ it only allows numbers but you can put $0. When I use /^[1-9\b]+$/ it doesn't let you put in a zero but if you type 1 and try to put a zero it won't let you. When I try /^[1-9\b]+/, it doesn't allow zero but now you can type text and characters. Is there a better way to do this?

const re = /^[0-9\b]+$/ is the current regex I'm using.

This is in ReactJS

General Grievance
  • 4,555
  • 31
  • 31
  • 45

3 Answers3

0

Try this

<input type="number" min="1" step="any" />
Nagween Jaffer
  • 180
  • 3
  • 13
0

With regex you can use that:
^[^0]\d+" And add the rest of your usage, this will avoid 0 being in the begining. [^] match any character that is not in the set - so putting 0 inside will avoid the first char to be 0 (with [^0]).

LironShirazi
  • 381
  • 1
  • 8
0

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      financialGoal: ''
    }
  }
  
  handleChange(evt) {
    const financialGoal = (evt.target.validity.valid) ? evt.target.value : this.state.financialGoal;
    
    this.setState({ financialGoal });
  }
  
  render() {
    return (
      <input type="number" pattern="[0-9]*" min="1" step="any" onInput={this.handleChange.bind(this)} value={this.state.financialGoal} />
    )
  }
}
  
ReactDOM.render(<Example />, document.body);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>