Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

javascript - 检查输入的是数字还是字母javascript(Check if input is number or letter javascript)

I'm using forms in HTML and javascript.(我正在使用HTML和javascript中的表单。)

I would like an alert to pop up only if the user inputs a LETTER and clicks submit .(我想一个警告弹出只有当用户输入一个字母 ,点击submit 。) So I have the HTML code:(所以我有HTML代码:) <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:(和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; } }   ask by Greg Peckory translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the isNaN function to determine if a value does not convert to a number.(您可以使用isNaN函数确定值是否不转换为数字。)

Example as below:(示例如下:) function checkInp() { var x=document.forms["myForm"]["age"].value; if (isNaN(x)) { alert("Must input numbers"); return false; } }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...