How can I filter the number

Hi can I ask some help please, I have input field and I want to accept only if the beginning has 1100 or 1200

example: 1100200300

how can I achieve on this ?

Thank you in advance.

You can use a regular expression to achieve that without needing any JavaScript at all.

<input type="text" pattern="^(1100|1200).*" required>

Hi Paul thank you,

Is this will support all browsers?

Hey jemz,

You can easily check that via the Can I use… website: https://caniuse.com/input-pattern

Spoiler alert, the answer is yes (apart from Opera Mini).

HI James, thank you I’m new to this site https://caniuse.com/input-pattern, how can I use the site to test, sorry to ask

@James_Hibbard

what should I input on this field ?

Ok like this

Thank you Paul and James. I’ll be back I will try this

HI @Paul_Wilkins

Is it possible also to check if the length is exactly 10?

Thank you in advance

Yes of course, you just need to check that there are six other digits, and use the $ symbol to say that you expect no more characters after that.

<input type="text" pattern="^(1100|1200)\d{6}$" required>

Thank you so much paul works now… I missed that symbol and I used {10} instead of 6 I also used the asterisk I’m almost closed to it.

Thank you :slightly_smiling_face:

Is that because the total length is required to be 14 digits? The 1100 (or 1200) plus another 10?

If the total length is supposed to be 10 digits, then you’ll instead need the initial 4, plus another 6 after that.

By using the asterisk, you are not enforcing a length restriction. They could put in 30 digits without restriction.

The solution that I provided (with the first 4 being checked, and another 6) including the dollar symbol at the end, is for when the length can be no shorter than 10 and no longer than 10. That is, exactly 10 digits long.

1 Like

Paul! you shared the right post. Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.