0

Is it possible to use the isNaN() function to check if textbox value is a number without listing each field? I tried the below coded and it does not work properly. It triggers my alert regardless of what is input.

var numberCheck = function() {

    var i = this.value

    if(isNaN(i)==true) {
        alert("You must enter an number value!");
    }
}   
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
NewSpeaker
  • 67
  • 1
  • 3
  • 12
  • `isNaN()` attempts a numeric conversion. What is the value of `this`? Are you sure it's the text box? – cookie monster Mar 21 '14 at 18:47
  • It may be helplful to you http://stackoverflow.com/questions/6449611/how-to-check-whether-a-value-is-a-number-in-javascript-or-jquery – Suman Bogati Mar 21 '14 at 18:47
  • In JavaScript, 'this' is not always what it seams to be. –  Mar 21 '14 at 18:49
  • It's not clear what you're trying to do, but be aware that `isNaN()` is **not** for general numeric field format validation. It is specifically for checking for the `NaN` numeric (non-)value. – Pointy Mar 21 '14 at 18:51
  • 1
    Off-topic: you can use `if(isNaN(i))` instead of `if(isNaN(i)==true)` – Oriol Mar 21 '14 at 18:51
  • I want to only allow number values in my matrix but only for certain text boxes. There are a lot of textboxes and I want to avoid listing each one. – NewSpeaker Mar 21 '14 at 18:52
  • Not enough info in your question. What debugging have you done? Have you done `console.log(this.value)` to verify that the value is what you expect? How is the function being invoked? – cookie monster Mar 21 '14 at 18:53
  • I have not tried console.log but I will (didn't think of that). The function is being invoked via the onkeyup event. – NewSpeaker Mar 21 '14 at 19:00
  • When I tried to include this.value in my alert message it came up as undefined. What am I missing? – NewSpeaker Mar 21 '14 at 19:02
  • No one knows what you're missing because you've posted very little information, but it appears as though `this` is not a reference to your element. If you're calling the function like this: `numberCheck()`, then clearly it will have the wrong `this` value. But I don't know how/where you're calling it, so I'm afraid I can't help unless you provide more information as was requested. I could guess at the solution based on what I think your code probably looks like, but I don't want to. – cookie monster Mar 21 '14 at 19:15
  • I have a matrix with text input boxes. Some of these textboxes may only have a number value. The above function numberCheck() is supposed to alert the user when they have entered a non-number value. The function works as intended when I replace this.value with document.getElementById("myID").value. The function is triggerred on the onkeyup event of each textbox that I want to be numeric only. What I want to avoid is listing the 100+ textboxes by id. I hope that is a little more clear. – NewSpeaker Mar 21 '14 at 19:26
  • There's no question about what you want. The problem is you have a bug in your code, but have provided no code that demonstrates the bug. When you ask a question, you need to take the time to create a *minimal* yet *fully functional* demonstration of the problem. – cookie monster Mar 21 '14 at 19:47

1 Answers1

0

I figured out how to do what I wanted to do. I had to define the parameters of the function as the ID of the element.

onkeyup = "numberCheck(this.id);"

    var numberCheck = function(myID){

if(isNaN(document.getElementById(myID).value)){
    alert("You must enter an number value!" );

 };
}   
NewSpeaker
  • 67
  • 1
  • 3
  • 12