How can I make regex pattern with characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and &

How can I make regex pattern allowing following characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and &

                  <textarea
                    id="comments"
                    type="textarea"
                    placeholder='comments'
                    {...register("comments", {
                        required: true,
                        minLength: {
                            value: 5,
                            message: "Minimum length of 5 letters"
                        },
                        pattern: {
                            value: /^[a-z0-9]+$/i,
                            message: "Supports characters, numbers, white space, "", ?, !, @, & and commas"
                        }
                    })}
                    >
                  </textarea>

>Solution :

Change

value: /^[a-z0-9]+$/i,

to

value: /^[a-z0-9\s"?!,\-.@&]+$/i,

\s is whitespace, and - needs to be escaped so it’s not treated as the range separator. The rest is just the other characters you said you want to allow.

Leave a Reply