ThurstonLevi
ThurstonLevi

Reputation: 859

using a pattern in html input for just numbers and N/A

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="">

Upvotes: 0

Views: 1207

Answers (1)

Damiano
Damiano

Reputation: 811

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 (+)

Upvotes: 1

Related Questions