Develop ultra fast with Prismane 🎉

regex

This validator checks if a string matches a regular expression pattern.

Basic Usage

import { regex } from "@prismane/core/validators";

console.log(regex("martinpetrov", /^[a-z]+$/)); // Will return null
console.log(regex("MARTIN", /^[a-z]+$/)); // Will return an error`

useForm Hook Usage

function Demo() {
  const { handleSubmit, handleReset, register } = useForm({
    fields: {
      username: {
        value: "",
        validators: {
          regex: (v: string) => regex(v, /^[a-z]+$/),
        },
      },
    },
  });

  return (
    <Form
      onSubmit={(e: any) => {
        handleSubmit(e, (v: any) => console.log(v));
      }}
      onReset={() => handleReset()}
      maw={300}
    >
      <TextField
        placeholder="eg. ivanivanov"
        label="Username:"
        {...register("username")}
      />
      <Button type="submit">Submit</Button>
    </Form>
  );
}

API

Parameters

NameTypeDescription
valuenumberThe number that will be validated.
regExpRegExpThe regex that will be matched.
fieldNamestring / undefinedThe name of the field.