检查输入是数字还是字母javascript [英] Check if input is number or letter javascript
本文介绍了检查输入是数字还是字母javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在HTML和JavaScript中使用表单。我只想在用户输入 LETTER 并点击 submit
时弹出提醒。
所以我有HTML代码:
< form name =myFormaction =onsubmit = return checkInp()method =post>
名:< input type =textname =age>
< input type =submitvalue =提交>
和javascript代码:
函数checkInp()
{
var x = document.forms [myForm] [age] .value;
if(x由任何字母组成)//这是我需要更改
{
alert(必须输入数字)的代码;
返回false;
$ div $解析方案
你可以使用isNaN函数来确定值是否不转换为数字。示例如下:
函数checkInp()
{
var x = document.forms [myForm 。 ] [ 年龄]值;
if(isNaN(x))
{
alert(必须输入数字);
返回false;
}
}
I'm using forms in HTML and javascript. I would like an alert to pop up only if the user inputs a LETTER and clicks submit
.
So I have the HTML code:
<form name="myForm" action="" onsubmit="return checkInp()" method="post">
First name: <input type="text" name="age">
<input type="submit" value="Submit">
And the javascript code:
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;
}
}
解决方案
You can use the isNaN function to determine if a value does not convert to a number. Example as below:
function checkInp()
{
var x=document.forms["myForm"]["age"].value;
if (isNaN(x))
{
alert("Must input numbers");
return false;
}
}
这篇关于检查输入是数字还是字母javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文