test alphanumeric string

M

Matt

I want the javascript to test an alphanumeric (a string contains
alphabet or numbers only) string. Should I write a regular expression?
What's the best way to do? please help. thanks
 
K

kaeli

I want the javascript to test an alphanumeric (a string contains
alphabet or numbers only) string. Should I write a regular expression?
What's the best way to do? please help. thanks

From my validation.js file...

function checkAlphanum(strObject)
{
/* Returns true if the field has all alphanumeric characters, false if
not.
You must pass in a input (text) object, not the value. */
var re = /^[A-Za-z0-9]+$/;
if (!strObject || isBlank(strObject)) return false;
else return re.test(strObject.value);
}

function isBlank(strObject)
{
/* Returns true if the field is blank, false if not.
You must pass in an input (text) object (not the value) */
var re = /\S+/;

if (!strObject) return true;
return re.test(strObject.value));
}

HTH
--
--
~kaeli~
If that phone was up your a$$, maybe you could drive a
little better!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
M

Michael Winter

[snip]
function checkAlphanum(strObject)
{
/* Returns true if the field has all alphanumeric characters,
false if not.
You must pass in a input (text) object, not the value. */
var re = /^[A-Za-z0-9]+$/;
if (!strObject || isBlank(strObject)) return false;
else return re.test(strObject.value);
}

Though the call to isBlank is redundant. As far as I can see, it would
only be of significance if checkAlphanum allowed whitespace but you wanted
to ensure that the value didn't contain only whitespace.

[snip]

Mike
 
K

kaeli

function isBlank(strObject)
{
/* Returns true if the field is blank, false if not.
You must pass in an input (text) object (not the value) */
var re = /\S+/;

if (!strObject) return true;
return re.test(strObject.value));

Whoops, this should be
return !re.test(strObject.value));


--
--
~kaeli~
The Bermuda Triangle got tired of warm weather. It moved to
Finland. Now Santa Claus is missing.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top