[英]Check if input of contenteditable div is numeric
I have a contenteditable div that is to be used to input numeric values. 我有一个contenteditable div,该div可用于输入数字值。 If the value entered is not numeric, I want a message to alert saying that the value is not valid. 如果输入的值不是数字,我希望收到一条消息提醒说该值无效。
html: HTML:
<div class="myinput" contenteditable></div>
JS: JS:
if (myinput is not a number)
alert("Please enter a valid number.");
What do I replace "myinput is not a number" with to get what I want? 我怎样用“ myinput is a number”代替“ myinput is a number”以得到我想要的东西? I tried using !$("div.number").html() but that only checks to see if any input exists, not a numeric one. 我尝试使用!$(“ div.number”)。html(),但这仅检查是否存在任何输入,而不是数字输入。 I am not sure if it is possibile to use .isNumeric() after .html(). 我不确定.html()之后是否可以使用.isNumeric()。 I must also keep the input as a div, I cannot use an input type="number". 我还必须将输入保留为div,我不能使用输入type =“ number”。 Any help would be great. 任何帮助都会很棒。 Thanks! 谢谢!
You can use isNaN
: 您可以使用isNaN
:
if (isNaN($("div.number").html()))
Note that this also accepts strings with spaces at end (which is probably your case). 请注意,这也接受结尾处带有空格的字符串(可能是您的情况)。
To get the number after that, the simplest solution is 要获得此数字,最简单的解决方案是
var n = +$("div.number").html();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.