how to use javascript to check if the first character in a textbox is a number?

24,299

Solution 1

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
}

Solution 2

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.

Solution 3

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.

Share:
24,299
Dora
Author by

Dora

learning to be a web developer. Just started so much to learn. Anyone need volunteers I need some work experience :P

Updated on July 09, 2022

Comments

  • Dora
    Dora almost 2 years

    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.

    • hd1
      hd1 about 11 years
    • saji89
      saji89 about 11 years
      You could either use string functions or regular expressions.
    • Ry-
      Ry- over 9 years
      if (/^\d/.test(document.forms[0].elements[2].value))
  • Havenard
    Havenard about 11 years
    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.
  • vdua
    vdua about 11 years
    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
    vdua about 11 years
    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
  • lib3d
    lib3d about 11 years
    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.