Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

parseint - Checking if a variable is an integer in javascript

I made a form where the user inputs values for width and height that they want for the pop up window to be. I am using window.open for that.

So I think I need to check if the values for width and height are integer. I have a function that checks that a variable is an integer that is...

function isInteger(possibleInteger) {
    return !isNaN(parseInt(possibleInteger));
}

but I don't know how to call this function to the width and height function to check if the user inputted an integer. Can any one help?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This is an answer to question mentioned in the topic, not the actual one in the body of the text :).

The following method is more accurate on determining if the string is a real integer.

function isInteger(possibleInteger) {
    return /^[d]+$/.test(possibleInteger)?;
}

Your current method validates "7.5" for instance.

EDIT: Based on machineghost's comment, I fixed the function to correctly handle arrays. The new function is as follows:

function isInteger(possibleInteger) {
        return Object.prototype.toString.call(possibleInteger) !== "[object Array]" && /^[d]+$/.test(possibleInteger);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...