0
Regex only allow numbers & one dot on Input
Hi! My code allow only numbers but unlimited dots on my input. How can I allow only one dot? I want to accept 231.555, .5556 & 0.556884. But not like 1.55.44, 0.5.0, 1.000.000 and so on. Can I just make som change on my expression in my const to make it work? Thank you! https://code.sololearn.com/WT1YWjyF42Gi/?ref=app
21 Respostas
+ 1
Simba
try this. Something I found on Stackoverflow.
https://code.sololearn.com/WD53v7d1h5tX/?ref=app
+ 2
Bob_Li the code you found works identically and behaves like the implementation from my code. Have you tested what happens when you press dot several times? The input is deleted bit by bit. I could not improve it in my code so far.
+ 2
Yes Bob_Li the input type="text" stayed. I tested your code from the marked best answer.
+ 1
Simba you wrote āI canāt get it to work with my code when I try to implement parts of your codeā.
I donāt know how you try it to implement and what you want to achieve. How can anybody help without sufficient information?
+ 1
Bob_Li Thank you so much. I just deleted inputmode numeric to get the full keyboard on mobile too so I can use dot there also.
+ 1
JaScript Thatās true. Sorry!
+ 1
Bob_Li On my iphone I just got 0-9 unfortunately. But it's a matter of taste how you want it. :) I shortened the code and used addEventListner because I want to practice using it and it works exactly as I want now. Thank you mate!
+ 1
Ok, Thank youš
+ 1
Bob_Li update to my above post: this happend when you remove inputmode="numeric" and on iPad or iPhone and in Sololearn editor. Other tests I didnāt yet made.
0
[number]theDot[number]
something like that
0
JaScript I really appreciate your time and help. However, I can't get it to work with my code when I try to implement parts of your code. I am a beginner in javascript and regex. But I'll keep trying. Args in your code return true or false. Iām thinking I need to use event.preventDefault() for all false then? To prevent to put someting else than numbers and more than one dot in my input or am I thinking completely wrong?
0
JaScript Okey thank you. Can you not see my codebit in my question or do I need to make it public? I appreciate if you could take a quick look. Cheers!
0
Simba
I thought you just wanted numbers and dot? Full keyboard seems redundant, since letters and symbols cannot be used. The numbers keyboard have a dot.
Maybe different devices have different virtual keboards?
0
Iām beginner and I donāt know what you wanted to do but I did this. Is it working or not check!!. Sorry if Iām wrong Iām completely new to programming .
https://code.sololearn.com/W6f6IUht0r8u/?ref=app
0
Harsh Patel
???
" Iām beginner and I donāt know what you wanted to do but I did this. ."
???
You could at least change the name....š
0
Harsh Patel
and also delete the tags inside...š
Also perhaps a thank you to the original post author...
I hope you read and try to understand the code and maybe learn something, instead of just faking it.
0
Harsh Patel
ok, first you should not say something is yours if it is someone else's code. It's dishonest and misleading.
Also, if you have questions or want to learn something, start a new post. This way, you do not mess up other people's post. We are happy to help you.
also, there is no inputmode="float"
The values are "none", "text", "decimal",
"numeric", "tel", "search", "email","url"
0
JaScript
I cannot see the input deletion in my code. Looking at the code, there is no reason why it should do so.
Perhaps you could post your code?
I know there is a bug if you set the input type="number". The input field gets cleared when + or . is pressed. That is why you must use input type="text".
inputmode="numeric" calls the numbers keyboard for mobile devices, but it is still type="text".
https://stackoverflow.blog/2022/09/15/why-the-number-input-is-the-worst-input/
0
JaScript
is input type="text" ?
maybe it's an apple thing? cannot replicate that behavior in android.