•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 330,373 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,859 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 361 | Replies: 5
![]() |
•
•
Join Date: May 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Hi There.
I'm new in java script , and I want to know somthing in it.
I'm asking if i have a variable and i want to know the data type for the data in it.
I'm not sure if you understand me well , but i will give you an example:
If I have a variable named X , and this virable hold some data , how can I know
the Type for that data (int , char , bool , float , .......).
and if poseple show me that in example.
thank you.
mosh.
I'm new in java script , and I want to know somthing in it.
I'm asking if i have a variable and i want to know the data type for the data in it.
I'm not sure if you understand me well , but i will give you an example:
If I have a variable named X , and this virable hold some data , how can I know
the Type for that data (int , char , bool , float , .......).
and if poseple show me that in example.
thank you.
mosh.
•
•
Join Date: Mar 2008
Location: South Africa
Posts: 107
Reputation:
Rep Power: 1
Solved Threads: 11
•
•
Join Date: Mar 2008
Location: South Africa
Posts: 107
Reputation:
Rep Power: 1
Solved Threads: 11
Adding to that!!!! JavaScript are loosely typed, which means that the programmer does not need to specify the type of data that a variable will store when the programmer declares it, The language automaticaly adjust the variable's storage space to conform to the data values that the programmer assign to it, so A loosely Typed language automaticaly converts a variable to the correct data type within commands that perfoms operations such as adding two values
Being bored is an insult to oneself
> If I have a variable named X , and this virable hold some data , how can I know
> the Type for that data
The typeof operator should make things a bit easier for you. Try this:
> the Type for that data
The typeof operator should make things a bit easier for you. Try this:
<script type="text/javascript"> var i = 0; alert(typeof i); // number i = "0"; alert(typeof i); // string i = true; alert(typeof i); // boolean i = [1, 3]; alert(typeof i); // object </script>
Last edited by ~s.o.s~ : 5 Days Ago at 1:41 pm.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
•
•
•
•
Hi There.
~s.o.s~ & Traicey Thank you , what ~s.o.s~ write is the nearest of what i want.
all i want is to know the type of data that entered by user. for example if the user input was string , then give him a message
that the type is unacceptable.
If you're taking your input from a form, it will always be of type "string".
What you probably want is to know what the string contains and if the range of characters are acceptable.
You'd want to use some regular expression matching:
<script>
var input = prompt('give me a number');
if (input.search(/^[0-9]+$/) != -1) {
alert('i said a number !');
} else {
alert('thank you!');
}
</script>here is docs on the search method for strings.
The regex for checking is string is a number is: /^[0-9]+$/
This makes sure all the characters given are numbers [0-9] from start (^) to finish ($).
You can do the same for alphabetic characters: /^[a-z]+$/i
All characters are alphabetic from[ a-z] while ignoring case (/i).
Or put the two together:
/^[0-9a-z]+$/i
or add a few other allowed characters:
/^[0-9a-z_-\s]+$/i
which allows the _ character, the - character and the space character \s.
Heres an example with those put together:
var str = '24s';
if (str.search(/^[a-z]+$/i) != -1) {
alert('It is alphabetic');
} else if (str.search(/^[0-9]+$/) != -1) {
alert('It is numeric.');
} else if (str.search(/^[0-9a-z]+$/i) != -1) {
alert('It is alphanumeric.');
} else if (str.search(/^[0-9a-z\s]+$/i) != -1) {
alert('It is alphanumeric and has a space or two.. words?');
} else {
alert('unknown');
}You can also do:
eg:
// we want a number
var input = 'hello';
if (parseInt(input) != input) {
alert('input is not a number');
}for ints and floats..
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
DaniWeb Marketplace (Sponsored Links)
•
•
•
•
access advice broadband business classification code combo crime cult of the dead cow daniweb data data protection data transfer database drive dropdownlist encryption europe forensic forensics gadget google government hacking hard hardware help hitachi hp industrial espionage information internet linux mobile module net news payment services privacy protection reuse search security spot storage terabyte tutorials and more tv web wikipedia
- Working with SQL server's Image data type (ASP.NET)
- Check data type input!! (C)
- why is double data type giving an garbage value (Java)
- Problems with changing values returned by RegQueryValueEx (C)
- help on data type conversion (Visual Basic 4 / 5 / 6)
- Help With Data-type Declaration Symbols (Pascal and Delphi)
- Working with SQL's Text data type (ASP.NET)
- Text data type troubles in T-SQL (Database Design)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Exporting data to Excel
- Next Thread: Javascript moving stuff



Linear Mode