8

I'm making a simple form and having a textbox for street address.... All I want to do is check if the first value entered is a number or not.

How can I do it?

if(document.forms[0].elements[2].value.

that is all I have now but I'm not sure what I should add to it to check the first character only.

Sam
  • 7,252
  • 16
  • 46
  • 65
Dora
  • 6,776
  • 14
  • 51
  • 99

3 Answers3

11

As you said in your question you want to check for the first character only, you can use charAt function for string to check whether the first character is from 0 to 9 or any other check you want for the first character

Possible solution

var firstChar = document.forms[0].elements[2].value.charAt(0);
if( firstChar <='9' && firstChar >='0') {
      //do your stuff
}
vdua
  • 1,281
  • 1
  • 14
  • 24
  • 1
    Your original code would do the trick, this one wont. `firstChar` is not a number, its a string, and cannot be compared against `9` or `0`. This comparison will always be false. Compare against `'9'` and `'0'` instead, or get the ASCII value with `charCodeAt(0)` to be checked if between `48` and `57`. – Havenard Apr 01 '13 at 05:11
  • That would have worked as well. Try this in console. `str="90";c=str.charAt(0);c<=9 && c>=0` will return true. Javascript doesn't have strong types, hence == will work fine and '9' will return true when comparing with integer 9. Your answer would have been correct in other languages such as Java and C. Remember there are no static data types in JavaScript. – vdua Apr 01 '13 at 05:26
  • in chrome it returns true for `str="90";c=str.charAt(0);c<=9 && c>=0` while returns false for `str="a0";c=str.charAt(0);c<=9 && c>=0` – vdua Apr 01 '13 at 05:36
  • 1
    The String prototype has a valueOf method which tries to extract a number from the string. When a string is compared to a number, this method is called and then "55"<56 is true. When added, that's the toString method from the number which is called and so concatenation operates. And all this is not a browser specific thing. – atondelier Apr 01 '13 at 05:57
9

This can simply use isNaN. Just put a bang before it to check if it is a number, instead of isNaN's normal use of checking if it isn't a number, like so:

var val = document.forms[0].elements[2].value;
if (!isNaN(val.charAt(0))){ //If is a number
  //Stuff
}

This also goes with numbers as strings, so need to worry about quotes or any of that hoo ha.

1

You can use if (document.forms[0].elements[2].value.match(/^\d+/)) to check if the beginning of the field is composed by numbers.

It will match for:

0 - valid
1 - valid
1a - valid
1 a - valid
1234567 - valid
a - invalid
a1 - invalid

Literally anything that start with numbers.

You can extend its functionality to if (document.forms[0].elements[2].value.match(/^\d+ +.+/))

In this form it will now require that its a number, plus one or more spaces, followed by anything else.

0 - invalid
1 - invalid
1(space) - invalid
1 1 - valid
1 a - valid
12345 abcdef - valid

Read more about Regular Expressions to elaborate complexier checkings.

But remember first that not every address has numbers, and most countries in the world don't use this format of writing addresses. As for the address field, I believe you should leave it open to be written in however format the user wish.

Havenard
  • 27,022
  • 5
  • 36
  • 62