User Name Password Register
DaniWeb IT Discussion Community
All
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 374,613 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,378 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

Data Type

Join Date: Sep 2005
Posts: 611
Reputation: digital-ether will become famous soon enough digital-ether will become famous soon enough 
Rep Power: 5
Solved Threads: 38
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: Data Type

  #6  
May 10th, 2008
Originally Posted by mosh View Post
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!
Reply With Quote  
All times are GMT -4. The time now is 8:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC