469,291 Members | 1,786 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,291 developers. It's quick & easy.

alphanumeric space validation

Hi

How i check alphnumeric & space validation for input text ?
e.g.
input : "abc GNM 2" is valid &
input : "abc GNM %2" is invalid

guide me.

Mar 5 '07 #1
3 16453
pradeep wrote:
How i check alphnumeric & space validation for input text ?
e.g.
input : "abc GNM 2" is valid &
input : "abc GNM %2" is invalid
var str = '4uP m'
if (/^[a-z\s\d]*$/i.test(str)) {
alert('OK')
} else {
alert('not OK')
}

Hope this helps,

--
Bart

Mar 5 '07 #2
On Mar 5, 4:31 am, "pradeep" <pwprad...@gmail.comwrote:
Hi

How i check alphnumeric & space validation for input text ?
e.g.
input : "abc GNM 2" is valid &
input : "abc GNM %2" is invalid

guide me.
Bart is right, using regular expressions is the way to go, but if you
don't really understand them (yet), here's a describing example of
your solution :
/**
* The function receives a string as first
* parameter and test if it only contains
* letters, numbers and spaces, and returns
* true. If any other characters are found,
* it returns false
*/
function alphaNumericValidator(str) {
// if empty fields are invalid,
// replace the * by a +
// * = if any or more
// + = 1 or more
// \w = a letter (= [a-zA-Z])
// \d = a number (= [0-9])
// \s = white space (= [ \n\r\t])
// \n = new line
// \r = carriage return
// \t = tabs
// ^ = the beginning of the string
// $ = the end of the string
//
// [\w\d\s] = [a-zA-Z0-9 \n\r\t]
return /^[\w\d\s]*$/.test(str);
}
/**
* Test case
*/

// strings to test; these values
var test1 = 'abc GNM 2';
var test2 = 'abc GNM %2';

if ( alphaNumericValidator(test1) ) {
alert( "test1 is valid" );
} else {
alert( "test1 is invalid" );
}

if ( alphaNumericValidator(test2) ) {
alert( "test2 is valid" );
} else {
alert( "test2 is invalid" );
}
-yanick

Mar 6 '07 #3
Yanick wrote:
On Mar 5, 4:31 am, "pradeep" <pwprad...@gmail.comwrote:
Hi
How i check alphnumeric & space validation for input text ?
e.g.
input : "abc GNM 2" is valid &
input : "abc GNM %2" is invalid
guide me.

Bart is right, using regular expressions is the way to go, but
if you don't really understand them (yet), here's a describing
example of your solution :

[...]
return /^[\w\d\s]*$/.test(str);
[...]
\w allows underscore.

--
Bart

Mar 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Matt | last post: by
6 posts views Thread by ironcito | last post: by
2 posts views Thread by VB Programmer | last post: by
7 posts views Thread by kanepart2 | last post: by
pradeepjain
6 posts views Thread by pradeepjain | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.