Check if input is number or letter javascript

2019-01-08 12:04发布

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;
}
}

11条回答
Evening l夕情丶
2楼-- · 2019-01-08 12:34

Just find the remainder by dividing by 1, that is x%1. If the remainder is 0, it means that x is a whole number. Otherwise, you have to display the message "Must input numbers". This will work even in the case of strings, decimal numbers etc.

function checkInp()
{
    var x = document.forms["myForm"]["age"].value;
    if ((x%1) != 0) 
    {
        alert("Must input numbers");
        return false;
    }
}
查看更多
Deceive 欺骗
3楼-- · 2019-01-08 12:34
function isNumber(data){
    data = data +"e1";                      // Disallow eng. notation "10e2"+"e1" is NaN
    var clean = parseFloat(data,10) / data ; // 1 if parsed cleanly
    return (clean && (data/data) === 1.0);  // Checks for NaN
}

function isInteger(data){
    data = data +"e1";                      // Disallow eng. notation "10e2"+"e1" is NaN
    var clean = parseInt(data,10) / data ; // 1 if parsed cleanly
    return (clean && (data%1) === 0);          // Checks For integer and NaN
}
查看更多
爷的心禁止访问
4楼-- · 2019-01-08 12:35

A better(error-free) code would be like:

function isReallyNumber(data) {
    return typeof data === 'number' && !isNaN(data);
}

This will handle empty strings as well. Another reason, isNaN("12") equals to false but "12" is a string and not a number, so it should result to true. Lastly, a bonus link which might interest you.

查看更多
\"骚年 ilove
5楼-- · 2019-01-08 12:37

Try this:

if(parseInt("0"+x, 10) > 0){/* x is integer */}
查看更多
贪生不怕死
6楼-- · 2019-01-08 12:39

Use Regular Expression to match for only letters. It's also good to have knowledge about, if you ever need to do something more complicated, like make sure it's a certain count of numbers.

function checkInp()
{
    var x=document.forms["myForm"]["age"].value;
    var regex=/^[a-zA-Z]+$/;
    if (!x.match(regex))
    {
        alert("Must input string");
        return false;
    }
}

Even better would be to deny anything but numbers:

function checkInp()
{
    var x=document.forms["myForm"]["age"].value;
    var regex=/^[0-9]+$/;
    if (x.match(regex))
    {
        alert("Must input numbers");
        return false;
    }
}
查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-08 12:43

I know this post is old but it was the first one that popped up when I did a search. I tried @Kim Kling RegExp but it failed miserably. Also prior to finding this forum I had tried almost all the other variations listed here. In the end, none of them worked except this one I created; it works fine, plus it is es6:

    let regex = new RegExp(/[^0-9]/, 'g');
    let x = document.forms["myForm"]["age"].value;

    if (x.match(regex)) {
       alert("Must be a valid number");
            return
    }
查看更多
登录 后发表回答