Istar_Eldritch
Istar_Eldritch

Reputation: 220

Regex not working in HTML5 pattern

So I have this regex intended to let pass all text but those that contain as initial chars the "34" sequence:

^(?!34)(?=([\w]+))

The regex is working fine for me in https://regex101.com/r/iN1yN3/2 , check the tests to see the intended behavior.

Any Idea why it isn't working in my form?

<form>
    <input pattern="^(?!34)(?=([\w]+))" type="text">
    
        <button type="submit">Submit!</button>
</form>

Upvotes: 1

Views: 718

Answers (1)

mcrumley
mcrumley

Reputation: 5700

The pattern attribute has to match the entire string. Assertions check for a match, but do not count towards the total match length. Changing the second assertion to \w+ will make the pattern match the entire string.

You can also skip the implied ^, leaving you with just:

<input pattern="(?!34)\w+" type="text">

Upvotes: 4

Related Questions