+ 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
3 Respuestas
+ 3
You need to trim your input which removes white spaces from the end and beginning.
var p = prompt("Enter a Number:").trim();
+ 1
ok but now it says it can't read property trim of null if I press cancel
how do I handle this?
+ 1
if(p != null) {
p = p.trim();
}