944,034 Members | Top Members by Rank

Ad:
Sep 4th, 2006
0

finding out variable type from html input type tag in javascsript

Expand Post »
Hi,
I'm trying to get the data type of a value entered into an input type text field in a form.By using typeof i am getting string always.Eventhough i was entered a number i'm getting the type of data as string.How can i check whether it is a number or not? throw java script???????please reply me.
thanks in advance....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amaravanich is offline Offline
12 posts
since Aug 2006
Sep 4th, 2006
0

Re: finding out variable type from html input type tag in javascsript

Javascript by it's nature treats everything as a string, certainly all HTML controls that suport a value it will always be a string. Well more accurately everything is an object, and whenever you read that object in your code it tends to be implicitly converted or cast as a string. You have to explicitly cast your varables to a number when you require that behaviour, using the Number(string value) function e.g.

HTML and CSS Syntax (Toggle Plain Text)
  1. var myNumber;
  2.  
  3. myNumber = Number(document.getElementById("myTextBox").value);
  4.  
  5. if(myNumber == NaN)
  6. alert("You must enter a number");


you can also consider parseFloat() and parseInt()
http://www.javascripter.net/faq/convert2.htm

Another usefull link:
http://www.w3schools.com/jsref/jsref_Number.asp
Last edited by hollystyles; Sep 4th, 2006 at 5:50 am. Reason: Extra material
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Sep 4th, 2006
0

Re: finding out variable type from html input type tag in javascsript

Thanku very much for ur reply.I'm trying like this but no result its resulted true for every input.can u please tell me wts wrong with this?
if(f.age.value!=""&&f.id.value!=""&&f.name.value!="")
{
var myNumber;
myNumber = Number(f.age.value);
if(myNumber == NaN)
{
alert("You must enter a number");
return false;
}else{return true;}

}

else{
if(f.age.value=="")
{
alert("you have to fill your age");
return false;
}
if(f.id.value=="")
{alert("you have to fill your id");
return false;
}

if(f.name.value=="");
{
alert("you have to enter name");
return false;
}

}
}
</script>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amaravanich is offline Offline
12 posts
since Aug 2006
Sep 4th, 2006
0

Re: finding out variable type from html input type tag in javascsript

Sorry my mistake try this instead:

HTML and CSS Syntax (Toggle Plain Text)
  1. if(isNaN(myNumber))
  2. return false;
  3. else
  4. return true;

see here:
http://www.w3schools.com/jsref/jsref_Number.asp
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: Small problem with backgrounds
Next Thread in HTML and CSS Forum Timeline: Small but Serius Probs with GET req. method !





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC