| | |
finding out variable type from html input type tag in javascsript
Please support our HTML and CSS advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Aug 2006
Posts: 12
Reputation:
Solved Threads: 0
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....
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....
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.
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
HTML and CSS Syntax (Toggle Plain Text)
var myNumber; myNumber = Number(document.getElementById("myTextBox").value); if(myNumber == NaN) 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
•
•
Join Date: Aug 2006
Posts: 12
Reputation:
Solved Threads: 0
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>
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>
Sorry my mistake try this instead:
see here:
http://www.w3schools.com/jsref/jsref_Number.asp
HTML and CSS Syntax (Toggle Plain Text)
if(isNaN(myNumber)) return false; else return true;
see here:
http://www.w3schools.com/jsref/jsref_Number.asp
![]() |
Similar Threads
- server problems? passing variable info... (ASP)
- need help finding a script or tool to use for a news update program. (PHP)
- Setting up a form in PHP (PHP)
- submiting info into a database for retrieval! (PHP)
- Accessing JavaScript objects in the Body Tag (JSP)
- Populating a fields on a form base on a selected item from a droplist. (PHP)
Other Threads in the HTML and CSS Forum
- Previous Thread: Small problem with backgrounds
- Next Thread: Small but Serius Probs with GET req. method !
| Thread Tools | Search this Thread |
appointments asp background backgroundcolor beta browser bug calendar cart cgi code codeinjection corporateidentity css design development displayimageinsteadofflash dreamweaver emailmarketing epilepsy explorer firefox flash form format google griefers hackers hitcounter hover html ide ie7 ie8 iframe image images internet internetexplorer intranet iphone javascript jpeg layout macbook maps marketshare microsoft mozilla multimedia navigationbars news offshoreoutsourcingcompany opacity opera optimization pnginie6 positioning problem scroll seo shopping studio swf swf. textcolor timecolor titletags url urlseparatedwords visual visualization web webdevelopment webform website windows7






