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 odpowiedzi
+ 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.