0

On he official page https://redux-form.com/6.6.1/examples/fieldlevelvalidation/

On the field "Age" is an example of how Redux-forms wrongly assumes the letter e can be a valid character on keyPress, instead of only allowing numbers.

I have tried the following validations, to exclude the letter e, with no success:

export const onlyNumber = (value) => {
  return /^[^e]$|[0123456789]$/i.test(value);
};

What could be a possible successful way of skipping Redux-Forms way of validating and actually being able to make a number-only (with 'e' excluded) field?

Daniela
  • 71
  • 1
  • 9
  • I don't get it. Per your regex, you wish to allow either **ANY** single char which is not `e` or you wish to allow everything as long as it ends in a digit? Rather than blaming Redux, it would be highly beneficial if you actually explained what you are trying to do and make sure to provide examples which you expect to validate and examples which should fail validation. Additionally, does you confusion stem from using Redux or just regex functions in JS in general or just regex? – MonkeyZeus Sep 16 '19 at 13:47
  • Hi @MonkeyZeus, with all due respect, I'm looking for help on an issue that is Redux-Forms, as I indeed mentioned earlier: "https://redux-form.com/6.6.1/examples/fieldlevelvalidation/ notice the Age field allows for 'e' as input". Clearly I'm looking for help on how to manually not allow the 'e' to pass through as input, on key press. – Daniela Sep 16 '19 at 14:12
  • I see, you are referring to exponents. If you want to make sure that only digits are entered then `^[0-9]+$` is what you need. – MonkeyZeus Sep 16 '19 at 14:27
  • Thank you @MonkeyZeus, unfortunately, since [0123456789]$ is the equivalent of [0-9]+$, it still doesn't work. I'm looking for any additional validations, but truth is it might require me to do a pure JS workaround, without involving regex anymore – Daniela Sep 16 '19 at 15:15
  • There must be something else going on with your code because if you run `/^[0-9]+$/i.test('7')` from the console then you get `true` but `/^[0-9]+$/i.test('1e1')` produces `false`. – MonkeyZeus Sep 16 '19 at 15:23
  • It's possible that redux is converting exponents into digits before invoking the validation but that would be an unexpected "feature" – MonkeyZeus Sep 16 '19 at 15:25

1 Answers1

0

It's a feature, the fact that it allows 'e'... Shout out to @iamandrewluca in Github, for replying to my question at: https://github.com/erikras/redux-form/issues/4538

Similar question, and responses / workarounds to forbid the 'e' from showing, in Stack overflow: https://stackoverflow.com/a/31706796/4671932

Documentation: https://html.spec.whatwg.org/multipage/input.html#number-state-(type%3Dnumber)

Daniela
  • 71
  • 1
  • 9