11

long story short, i was trying to validate a phone field. ive added the isNaN and parseInt for checking the " " in the field but that said

This below never validates to true..what am i missing?

if(isNaN(parseInt(phone))){
        error.text("Sorry but this phone field requires numbers only");
        return false;
    } else {
    return true;

    }

it always fails...it never reads true even when i enter a number in the field and submit. i always get the error mssg.

EDIT: I am testing input values from a form, phone is the name of the field.

L84
  • 45,514
  • 58
  • 177
  • 257
somdow
  • 6,268
  • 10
  • 40
  • 58
  • Works for me, e.g. with phone="5". What are your test input values? – Bergi Apr 17 '12 at 02:44
  • yeah sorry, im testing input vals from a form – somdow Apr 17 '12 at 02:45
  • You should include your html here in the question too. That might help us debug it. – webnesto Apr 17 '12 at 02:46
  • 1
    -1 for not including the actual values that you're testing. I'm *guessing* that `phone` is an HTML input element reference instead of the `.value` of it. – Phrogz Apr 17 '12 at 02:46
  • But which number did you enter that gave the error instead of true? – Bergi Apr 17 '12 at 02:47
  • 1
    I'm wondering if a domnode is getting evaluated rather than the form value - hence never returning true since parseInt on an object will always not be a number. – webnesto Apr 17 '12 at 02:49
  • lols @ Phrogz...thanks...i edited post. @ Bergi, i first seubmited the form to test for the "" and then just typed random numbers, and then letters and it always throws me the error mssg – somdow Apr 17 '12 at 02:49
  • @webnesto Yep you were correct, somehow i thought that just giving the field name an id would resolve it but it didnt. like it treated it as a whole "object" so i changed it to if(isNaN(parseInt(phone.val()))) and it now works – somdow Apr 17 '12 at 02:57

4 Answers4

14

Various ways to coerse JS strings to numbers, and their consequences:

Results of converting various strings using the above techniques
(source: phrogz.net)

I personally use *1 as it is short to type, but still stands out (unlike the unary +), and either gives me what the user typed or fails completely. I only use parseInt() when I know that there will be non-numeric content at the end to ignore, or when I need to parse a non-base-10 string.

Edit: Based on your comment, if using phone.val() fixed it then

  1. You were using jQuery (which you never mentioned, and should have), and
  2. You actually had/have a jQuery object, wrapping one or more DOM elements (probably just one).

Whenever you do var foo = $('…'); then the foo variable references a jQuery object of one or more elements. You can get the first actual DOM element from this via var fooEl = foo[0]; or var fooEl = foo.get(0);…but even then you still have a DOM element and not a particular property of that.

For form inputs, you need to get the .value from the DOM element, which is what the jQuery .val() method does.

Community
  • 1
  • 1
Phrogz
  • 296,393
  • 112
  • 651
  • 745
11

parseInt is a bit odd at times:

> parseInt("123-456-789")
123

Fortunately you can probably solve your case with:

> Number("123-456-789")
NaN
rfunduk
  • 30,053
  • 5
  • 59
  • 54
9

parseInt only returns NaN if the first character cannot be converted to a number.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
1

I've seen Number() suggested, but that will still allow things like -21 or 123.456. The best way to check for the absence of non-digits in a string is like this:

function hasNonDigit(str){
  return /\D/g.test(str.toString());
}

console.log(hasNonDigit("123-456-7890"));
console.log(hasNonDigit("1234567890"));
Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15