NgatungaEmily
NgatungaEmily

Reputation: 105

Html pattern for a specific pattern

I have to try to write a pattern that accepts a character in the form of S0000/0000/0000 or P0000/0000/0000 Where 0 is any number

This is what I have a try

  <input class="form-control" type="text" pattern="^\d{5}/\d{4}/\d{4}$" name="index_no" required>

But that pattern it only accepts 5 digits / 4 digits / 4 digits what I need is S0000/0000/0000 OR P0000/0000/0000

Upvotes: 3

Views: 84

Answers (3)

Ryszard Czech
Ryszard Czech

Reputation: 18611

Use

pattern="[SP]\d{4}/\d{4}/\d{4}"

You need no ^ and $ as the entire string should match pattern regex.

No need to escape / characters in a string regex.

Upvotes: 1

vicki
vicki

Reputation: 470

Use \ to escape special characters in html. Try this,

pattern="[SP]\d{4}\/\d{4}\/\d{4}"

You can test for correctness of regex expressions in this tester site

Upvotes: 1

Chandral
Chandral

Reputation: 448

Sorry I misunderstood your requirement initially, please try this edited HTML

<input class="form-control" type="text" pattern="S\d\d\d\d/\d\d\d\d/\d\d\d\d|P\d\d\d\d/\d\d\d\d/\d\d\d\d" name="index_no" required>

Upvotes: 1

Related Questions