我正在使用HTML和JavaScript中的表单。如果用户输入了字母并点击提交
按钮,我希望弹出一个警告框。
因此,我有以下HTML代码:
<form name="myForm" action="" onsubmit="return checkInp()" method="post">
First name: <input type="text" name="age">
<input type="submit" value="Submit">
并且 JavaScript 代码:
function checkInp()
{
var x=document.forms["myForm"]["age"].value;
if (x consists of any letters) // this is the code I need to change
{
alert("Must input numbers");
return false;
}
}
<input type="text" />
切换为<input type="number" min="0" max="125" step="1" />
。在现代浏览器中,这将检查数字输入而无需任何JS。 - fboes