Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 208
Dora Avatar asked Apr 01 '13 04:04

Dora


People also ask

How do you check if the first character of a string is a number JavaScript?

Use the isNaN() Function to Check Whether a Given String Is a Number or Not in JavaScript. The isNaN() function determines whether the given value is a number or an illegal number (Not-a-Number). The function outputs as True for a NaN value and returns False for a valid numeric value.

How do you check if a character is a number in JavaScript?

To check if a character is a number, pass the character as a parameter to the isNaN() function. The function checks if the provided value is NaN (not a number). If the function returns false , then the character is a valid number. Copied!

How do you check if an item is a number in JavaScript?

In JavaScript, there are two ways to check if a variable is a number : isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false. typeof – If variable is a number, it will returns a string named “number”.

How do you check if a string has a number JavaScript?

Use the RegExp. test() method to check if a string contains at least one number, e.g. /\d/. test(str) . The test method will return true if the string contains at least one number, otherwise false will be returned.


3 Answers

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
}
like image 51
vdua Avatar answered Oct 07 '22 19:10

vdua


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.

like image 39
Jordan Michael Rushing Avatar answered Oct 07 '22 19:10

Jordan Michael Rushing


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.

like image 41
Havenard Avatar answered Oct 07 '22 19:10

Havenard