4

I am using rexeg to allow only certain characters in a text field. The solution works great however when the field is left blank, the validation throws an error. Basically, I need to allow empty string in a field that is being validated with the below rule:

/^[0-9a-zA-Z,.()+\/\s-]+$/

I tried to put | and |null on different places in the regex rule - in the beginning, in the end, inside or outside the brackets but it does not work. It either allows all characters or still does not allow empty string. Can someone with more knowledge propose a solution please:

3 Answers 3

3

Just replace anchor + (1 or more matches) with * (zero or more matches)

Example:

/^[0-9a-zA-Z,.()+\/\s-]*$/      
Sign up to request clarification or add additional context in comments.

Comments

3

Replace the + at the end with a *.

+ means one to infinite repetitions of the previous token

* means zero to infinite repetitions of the previous token

Comments

2

You may try this:

/^[0-9a-zA-Z,.()+\/\s-]*$/
                       ^-- replace + with *

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.