-1

I use HTML5 patter to validate address field input. Here is example of my code:

<input class="form-control" type="text" name="address1" id="address1" value="" maxlength="40" required>

I would like to use pattern attribute to prevent PO box in the address field. Can anyone help me achieve that with patter regex?

espresso_coffee
  • 5,076
  • 9
  • 53
  • 124
  • Use a negative lookahead that matches all the forms of `PO Box` that you want to prohibit. – Barmar Jan 30 '20 at 22:54
  • @Barmar Can you please provide any example? – espresso_coffee Jan 30 '20 at 22:55
  • You'll have to provide more details. Addresses can vary _**widely**_ so you'll have to exactly define what you want to exclude. I've seen all the following variations (and I'm sure there are more) how a person has written a PO Box ... `PO Box 123`, `P.O. Box 123`, `P.O. Box #123`, `Box 123`, `POB 123` etc. upper/lowercase, spacing, with/without the `#` and so on. – Stephen P Jan 30 '20 at 22:55
  • @StephenP Any combination of PO box should be prevented. In the existing program they use this: `var reNewTest=/[Bb][Oo][Xx][\d]/ if(reNewTest.test(Addr1))` I'm not sure how loose is that regex. – espresso_coffee Jan 30 '20 at 22:59
  • Something like `.*(?![Bb][Oo][Xx]\s+\d)`. – Barmar Jan 30 '20 at 23:03

1 Answers1

0

Use a pattern with a negative lookahead. See Regular expression to match a line that doesn't contain a word and replace the word in those patterns with the pattern that matches box followed by a number.

.*(?![Bb][Oo][Xx]\s+\d)

<form>
  <input type="text" pattern="((?![Bb][Oo][Xx]\s+\d).)*">
  <input type="submit" value="Submit">
</form>
Barmar
  • 596,455
  • 48
  • 393
  • 495