finding out variable type from html input type tag in javascsript

Thread Solved

Join Date: Aug 2006
Posts: 12
Reputation: amaravanich is an unknown quantity at this point 
Solved Threads: 0
amaravanich amaravanich is offline Offline
Newbie Poster

finding out variable type from html input type tag in javascsript

 
0
  #1
Sep 4th, 2006
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....
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

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

 
0
  #2
Sep 4th, 2006
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
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 12
Reputation: amaravanich is an unknown quantity at this point 
Solved Threads: 0
amaravanich amaravanich is offline Offline
Newbie Poster

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

 
0
  #3
Sep 4th, 2006
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>
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

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

 
0
  #4
Sep 4th, 2006
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
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC