Regex only allow numbers & one dot on Input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

9th Nov 2022, 3:27 PM
Simba
21 Answers
+ 1
Simba try this. Something I found on Stackoverflow. https://code.sololearn.com/WD53v7d1h5tX/?ref=app
10th Nov 2022, 12:12 AM
Bob_Li
Bob_Li - avatar
+ 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.
11th Nov 2022, 1:07 PM
JaScript
JaScript - avatar
+ 2
Yes Bob_Li the input type="text" stayed. I tested your code from the marked best answer.
11th Nov 2022, 8:33 PM
JaScript
JaScript - avatar
+ 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?
10th Nov 2022, 8:17 AM
JaScript
JaScript - avatar
+ 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.
10th Nov 2022, 9:13 AM
Simba
+ 1
JaScript Thatโ€™s true. Sorry!
10th Nov 2022, 9:13 AM
Simba
+ 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!
10th Nov 2022, 9:44 AM
Simba
+ 1
Bob_Li This is my first day on Sololearn and I just started learning python. Just tried somthing else for fun. I was playing with your code and tried float in input type. I even dont know what Regex is. ๐Ÿ˜…๐Ÿ˜…๐Ÿ˜…
11th Nov 2022, 12:54 PM
Harsh Patel
Harsh Patel - avatar
+ 1
Ok, Thank you๐Ÿ˜Š
11th Nov 2022, 1:01 PM
Harsh Patel
Harsh Patel - avatar
+ 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.
11th Nov 2022, 3:10 PM
JaScript
JaScript - avatar
0
[number]theDot[number] something like that
9th Nov 2022, 4:25 PM
Arturop
Arturop - avatar
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?
9th Nov 2022, 6:25 PM
Simba
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!
9th Nov 2022, 7:33 PM
Simba
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?
10th Nov 2022, 9:35 AM
Bob_Li
Bob_Li - avatar
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
11th Nov 2022, 12:14 PM
Harsh Patel
Harsh Patel - avatar
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....๐Ÿ˜…
11th Nov 2022, 12:16 PM
Bob_Li
Bob_Li - avatar
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.
11th Nov 2022, 12:43 PM
Bob_Li
Bob_Li - avatar
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"
11th Nov 2022, 12:59 PM
Bob_Li
Bob_Li - avatar
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/
11th Nov 2022, 1:13 PM
Bob_Li
Bob_Li - avatar
0
JaScript is input type="text" ? maybe it's an apple thing? cannot replicate that behavior in android.
11th Nov 2022, 5:57 PM
Bob_Li
Bob_Li - avatar