0

What I want to do is just to accept valid number so if someone put text and click continue the application should display an error saying that just numbers are allowed and only for numbers these eE+- keys will not be accepted means single keys if user put just e and hit submit I need to throw an error. I am using Formik also

The following code does not display an error message if the textfield is empty and the other validation to check if the user inputs text or numbers is pending and I'm stuck.

like this way it needs to be supported

this is valid 
+1
-1
1e1

this blow is invalid need to throw error in this case
e
1-
1+
12e
const NumberField = (props) => {
  const { errorMessege, ...restProps } = props;

  const Number = /^-?(0|[1-9]\d*)(e-?(0|[1-9]\d*))?$/i;
  const numberConvertor = (e) => {
    if (e.target.value == '' || Number.test(e.target.value)) {
       return true
      console.log(e.target.value);
    } else {
      e.preventDefault();
       return false;
    }
  };

  return (
    <TextField
      fullWidth
      onKeyPress={numberConvertor}
      helperText={errorMessege}
      error={Boolean(errorMessege)}
      {...restProps}
    />
  );
};
yeti
  • 13
  • 4
  • Can't you use a library as a wrapper on the textfield provided by material ui, https://stackoverflow.com/questions/45758998/how-can-i-mask-my-material-ui-textfield please look here for some insights. – JJY9 May 31 '23 at 12:45

1 Answers1

0

According to your code:

https://codesandbox.io/s/sleepy-currying-5ib741?file=/NumberField.js

Chaneg the NumberField component to this:

const NumberField = (props) => {
  const { errorMessege, ...restProps } = props;

  const handleKeyDown = (e) => {
    const key = e.key;
    if (!/[\d+eE-]/.test(key)) e.preventDefault();
  };

  return (
    <TextField
      fullWidth
      onKeyDown={handleKeyDown}
      helperText={errorMessege ? errorMessege : ""}
      error={Boolean(errorMessege)}
      {...restProps}
    />
  );
};

export default NumberField;

and change formValidationSchema to this:

const formValidationSchema = Yup.object({
  value: Yup.string()
    .required("Please enter a number")
    .matches(
      /^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/,
      "Please enter a valid number."
    )
    .min(1, "Please enter a value between 1 and 35.")
    .max(35, "Please enter a value between 1 and 35.")
});
Ghaem
  • 53
  • 1
  • 9
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 31 '23 at 09:45
  • no, this is not what I am expecting this is not a solution – yeti May 31 '23 at 10:03
  • @yeti whats wrong with this answer? and can explain more that what do you want exactly? you want to show the error after clicking on the submit button? – Ghaem May 31 '23 at 10:05
  • I don't want the user to be able to type alpha characters – yeti May 31 '23 at 10:10
  • by using this I am able to type a to z characters – yeti May 31 '23 at 10:11
  • just user able to type numbers with symbols +,-, and e in a valid format, and for example i mention valid and invalid criteria – yeti May 31 '23 at 10:12
  • your updated code it's not returning true – yeti May 31 '23 at 10:49
  • @yeti did you test it? with this code, the user can only enter `numbers`, `+`, `-`, `e` and `E`. no part of the code needs to return true or false. what do you mean? – Ghaem May 31 '23 at 10:54
  • yes I test it so that fine user is only able to enter numbers +, -, e, and E but for invalid cases it needs to throw an error for example which case setError will become true – yeti May 31 '23 at 10:58
  • helperText={error ? 'Invalid number' : errorMessege} This case is not getting true errorMessegeis coming to this component form outside that I am handling based on the different requirement of this number filed – yeti May 31 '23 at 11:02
  • that's why I am saying setError is not becoming true – yeti May 31 '23 at 11:04
  • I put just e in that case setError not become true – yeti May 31 '23 at 11:04
  • @yeti `setError(value === '' || !numberRegex.test(value))` If the input is empty or if it does not match the regex, the `error` state value will be true and the `errorMessage ` value, which is a prop, will be displayed. – Ghaem May 31 '23 at 11:07
  • @yeti can you tell me how you handle `errorMessege` value? – Ghaem May 31 '23 at 11:07
  • errorMessege={ formikForm?.errors?.bins && formikForm?.touched?.bins ? formikForm.errors.bins : null } – yeti May 31 '23 at 11:10
  • I Just put e nothing else e is an invalid number but error is false – yeti May 31 '23 at 11:12
  • @yeti as I understand it, you are using a regex in formik, right? – Ghaem May 31 '23 at 11:16
  • yes I am using regex in formik I want to handle in this component it itself does not want to right unnecessary repeated code added code send box link check this – yeti May 31 '23 at 11:21
  • I am going to use this number field in many places – yeti May 31 '23 at 11:23
  • https://codesandbox.io/s/sleepy-currying-5ib741?file=/demo.js – yeti May 31 '23 at 11:25
  • every time unnecessary I need to do this .matches( /^[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?$/, "Please enter a valid number." ) Don't you think this is going to be repeated code @Ghaem – yeti May 31 '23 at 18:06
  • that I need to handle in onChnage then no need to right this thing again and again – yeti May 31 '23 at 18:09
  • @yeti you can do the validation on submit. – Ghaem Jun 01 '23 at 20:32
  • @Ghaem what do you mean by you can do the validation on submit? – yeti Jun 02 '23 at 10:26