[英]How can I change my number validation from a form to a prompt?
So currently I have this code: 所以目前我有这段代码:
<html>
<title>Number Seperation</title>
<body>
<p>Please enter as many numbers in the box below as you wish.
<br>
Make sure to seperate each different number by hitting the submit button.</p>
<form>
<script>
function isNumberKey(evt){
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
<input type="number" name="quantity" min="1">
<input type="submit">
</form>
</body>
Which has a text box where you can input only numbers. 其中有一个文本框,您只能在其中输入数字。 I would like to know how to change the text box to a prompt window while still keeping the validation check to ensure only numbers are accepted.
我想知道如何将文本框更改为提示窗口,同时仍保持验证检查以确保仅接受数字。 How would I do this?
我该怎么做?
I've just tied the getInput method to the button. 我刚刚将getInput方法绑定到按钮。 window.prompt pops up the input dialog.
window.prompt弹出输入对话框。 I used isNaN() to check whether it's (not) a number, but you can customize to your need.
我使用isNaN()检查它是否为数字,但是您可以根据需要进行自定义。 Then I'm concatenating it into the list element.
然后,将其串联到list元素中。
function getInput(){ var input = window.prompt("Enter a number"); if(isNaN(input)) alert('That was not a number') else { document.getElementById('list').innerHTML += "<li>" + input + "</li>"; } }
<html> <title>Number Seperation</title> <body> <p>Please enter as many numbers in the box below as you wish. <br>Make sure to seperate each different number by hitting the submit button.</p> <form> <input type="button" onclick="getInput()" value="Supply number" /> </form> <ul id="list"> </ul> </body>
References for things I used: 我使用过的东西的参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.