Svelte: Input does not validate correcetly according to pattern when the pattern contains numbers

Created on 23 Feb 2019  路  3Comments  路  Source: sveltejs/svelte

<input bind:value='number' placeholder='enter a number' pattern='^[0-9]{1,3}$'>

The above input does not accept input that is valid according to the regex pattern.
An equivalent code validates as expected in pure HTML and in other frameworks like Inferno.

<form onsubmit="alert('ok')">
  <input placeholder='enter a number' pattern='^[0-9]{1,3}$'>
</form>

All 3 comments

This is expected - {1,3} is being interpreted as a tag, which then is a javascript expression 1,3 which evaluates to 3. If you want to have something that looks like a tag, you can escape the { as &#123;.

You are right.
<input bind:value='number' placeholder='enter a number' pattern='^[0-9]&#123;1,3&#123;$'>
While ugly, the above validates correctly.
Thank you very much.

@juniorsd
you can use String.raw

Example:

<input type="text" name="cpf" pattern="{String.raw`\d{3}.\d{3}.\d{3}-\d{2}`}" required>

Output is:

<input type="text" name="cpf" pattern="\d{3}.\d{3}.\d{3}-\d{2}" required="">
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AntoninBeaufort picture AntoninBeaufort  路  3Comments

st-schneider picture st-schneider  路  3Comments

clitetailor picture clitetailor  路  3Comments

plumpNation picture plumpNation  路  3Comments

robnagler picture robnagler  路  3Comments