Empty string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Empty string?

I have this code. I want it to only accept numbers. I have put figured out, but how can I make it so as long as there's both nothing inputted, it will say "Please enter a number." This is worded horribly. An example would be if I press space twice, it returns that as a number and prints it to the screen. How can I fix this? https://code.sololearn.com/WteXhS8974vW/?ref=app

8th Jun 2018, 11:44 PM
Daniel Cooper
Daniel Cooper - avatar
3 Answers
+ 3
You need to trim your input which removes white spaces from the end and beginning. var p = prompt("Enter a Number:").trim();
8th Jun 2018, 11:53 PM
Toni Isotalo
Toni Isotalo - avatar
+ 1
ok but now it says it can't read property trim of null if I press cancel how do I handle this?
9th Jun 2018, 12:08 AM
Daniel Cooper
Daniel Cooper - avatar
+ 1
if(p != null) { p = p.trim(); }
9th Jun 2018, 1:08 AM
Toni Isotalo
Toni Isotalo - avatar