0

Struggling to get the correct formatting for a regex which will accept any number or N/A any other value would fail validation. The following did not work. My regex is less than average...

<input type="text" record="job" name="quote_design_hours" size="6" pattern="[0-9|N/A]" title="Value must be a number or N/A" value="">

1 Answer 1

1

Your pattern should be (N/A|[0-9]+)

Here is an example in action

The reasoning is:

  • value should match one of two expressions so you use OR operator | (you can use non-matching groups with (?: ... | ... ) instead
  • first expression is literally N/A
  • second expression is any digit [0-9] (i.e. any character from 0 to 9), and can be present one or more times (+)
Sign up to request clarification or add additional context in comments.

3 Comments

They are anchored by default.
Right, N/A|[0-9]+ will do. pattern="^(N/A|[0-9]+)$" will be translated into ^(?:^(N/A|[0-9]+)$)$ pattern. It will also work, but it is rather redundant.
Thanks, I edited my comment, I wasn't aware of this detail

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.