Javascript 检查用户插入的值是否高于0(使用js)
我从头开始创建一个自定义js验证器,不使用任何库 我想通过检查用户在输入标记中插入的内容是否大于0来验证数字输入: 这不管用吗: ifprice.value.match/^[0-9]+$/&&&+price.value>0{ 我不太确定哈希符号的用途,所以在我的示例中删除了它。除非您试图查找ID,您必须在其中查找元素,如下所示: var price=document.querySelectorprice;像这样的东西怎么样Javascript 检查用户插入的值是否高于0(使用js),javascript,Javascript,我从头开始创建一个自定义js验证器,不使用任何库 我想通过检查用户在输入标记中插入的内容是否大于0来验证数字输入: 这不管用吗: ifprice.value.match/^[0-9]+$/&&&+price.value>0{ 我不太确定哈希符号的用途,所以在我的示例中删除了它。除非您试图查找ID,您必须在其中查找元素,如下所示: var price=document.querySelectorprice;像这样的东西怎么样 您需要使用vanilla document.getElementById
您需要使用vanilla document.getElementById'price'访问您的输入元素。那么,您的问题是什么?检查使用纯vanilla jsprice的用户输入值高于0是否是无效语法,也不是访问DOM元素的推荐方法。使用document.GetElementyByDisprice。这不起作用。输出错误“无法读取属性”如果您对任何问题投反对票,请解释并提出一些建设性意见。
<label>Price
<input type="text" id="price" name="price">
</label>
function price()
{
if (#price.value>0)
console.log('0.');
else
console.log('Incorrect');
}
var price = document.getElementById("price").value;
if (parseInt(price) > 0 && !isNaN(price)))
{
// then do something
}
else
{
// else do something
}
function price()
{
var price = document.getElementById("#price"); // get the element
if (isNaN(price.value) || price.value <= 0) // if the value is not a number or is less than 0
console.log('Incorect');
else // otherwise
console.log('Correct');
}