Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regex pattern not working #236

Closed
2 tasks done
santhoshkarthi002 opened this issue Feb 29, 2024 · 3 comments
Closed
2 tasks done

regex pattern not working #236

santhoshkarthi002 opened this issue Feb 29, 2024 · 3 comments

Comments

@santhoshkarthi002
Copy link

santhoshkarthi002 commented Feb 29, 2024

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

  <TextFieldElement
        required
        margin="dense"
        name="Interval"
        fullWidth
        label="Duration"
        type="number"
        inputProps={{
          min: 1,
        }}
        validation={{
          pattern: {
            value: /^(0|[1-9]\d*)$/,
            message: 'Duration should have minimum of 1',
          },
        }}
      />

how can i put regex pattern to textfield element for validation

Expected behavior 🤔

No response

Steps to reproduce 🕹

Steps:

@sadik-malik
Copy link
Contributor

@santhoshkarthi002: The validation object is being passed to react-hook-form, but it is not triggering the error. This behavior persists even when using native HTML input.

Try using validate instead of pattern in validation prop.

validation={{
    validate: (value) => {
      if (!/^(0|[1-9]\d*)$/.test(value)) {
        return 'Duration should have minimum of 1';
      }
    },
  }}

@santhoshkarthi002
Copy link
Author

it,s working, Thank you.

@waza-ari
Copy link
Contributor

waza-ari commented Apr 12, 2024

Just for anyone finding this, with v7 the solution above is no longer valid. Check the v7 beta announcement (#252 ) for details. Essentially it must now be rules:

<TextFieldElement
  ...
  rules={{
    validate: (value) => {
      if (!/^$|^\d+[.,]?\d{0,2}$/g.test(value)) {
        return 'Please enter a valid number..';
      }
    }
  }}
/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants