473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

test alphanumeric string

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
Jul 23 '05 #1
4 11137
In article <ba**************************@posting.google.com >,
jr********@hotmail.com enlightened us with...
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

Jul 23 '05 #2
On Thu, 16 Sep 2004 13:01:01 -0500, kaeli <ti******@NOSPAM.comcast.net>
wrote:

[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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
In article <MP***********************@nntp.lucent.com>,
ti******@NOSPAM.comcast.net enlightened us with...

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

Jul 23 '05 #4
JRS: In article <ba**************************@posting.google.com >,
dated Thu, 16 Sep 2004 09:17:13, seen in news:comp.lang.javascript, Matt
<jr********@hotmail.com> posted :
I want the javascript to test an alphanumeric (a string contains
alphabet or numbers only) string. Should I write a regular expression?
Either that or copy one.
What's the best way to do? please help. thanks


<URL:http://www.merlyn.demon.co.uk/js-valid.htm#VP>, x1, adjust {6,10}.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Tonks | last post by:
OK, here's possibly a weird one. Is there any way to do string comparisons and ignore all non-alphanumeric characters? For instance, check "foobar" and have it match an existing record of "f$#!oo...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
6
by: ironcito | last post by:
Hello! I'm looking for a way to have a field in my database that will automatically be filled with a random 4-character alphanumeric string every time I enter a new record. Like an autonumber...
9
by: fooboo | last post by:
Does anyone know if a easier way (built in function, or something) that can verify that a string is an alphanumeric number? Here is what I am doing now: for(i=0; i < strlen(temp); i++){...
10
by: Crirus | last post by:
Is there a function that return some random ID like string alphanumeric? Like this: A35sDsd1dSGsH Thanks Crirus
7
by: Fernando Rodríguez | last post by:
Hi, How can I know if a string only has alfanumeric chars? Thanks
5
by: DotNetNewbie | last post by:
Hi, I want to parse a string, ONLY allowing alphanumeric characters and also the underscore '_' and dash '-' characters. Anything else in the string should be removed. I think my regex is...
3
by: Paul73 | last post by:
Hi everyone, I'm hoping someone can help me. I've created a windows app that inserts data into a sql database. The data comes from textboxes. This is a simple app that will only be used by...
1
by: shalabh6 | last post by:
Hi, A js file containing a function which is passing an alphanumeric string to another function in the same file, the second funtion requires 4 parameters to pass from the first function. The...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.