?
Solved

How do I fix this script so it validates alphanumeric and spaces ?

Posted on 2003-03-14
12
Medium Priority
?
169 Views
Last Modified: 2013-12-24
How do I fix this script so it validates alphanumeric and spaces ?

<cfscript>
function IsAN(str) {
  if (REFindNoCase("[^a-z0-9]", str) eq 0)
    return true;
  else
    return false;
}
</cfscript>

how do I make it accept "-" & "_" & "." as well as spaces ??

Thanks in advance.
0
Comment
Question by:markh51
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 

Author Comment

by:markh51
ID: 8139243
I've already seen examples like this, but don't understand what '^([0-9]{3}[-_./]?)?[0-9]{3}[-_. /]?[0-9]{4} is ??
0
 

Expert Comment

by:DynAstY
ID: 8139261
either do I but if i needed to i prob would good luck
0
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 

Author Comment

by:markh51
ID: 8139280
I've already seen examples like this, but don't understand what '^([0-9]{3}[-_./]?)?[0-9]{3}[-_. /]?[0-9]{4} is ??
0
 

Author Comment

by:markh51
ID: 8139283
Oppps, I pressed refresh too many times
0
 
LVL 8

Expert Comment

by:TallerMike
ID: 8139327
OK, here goes:

^([0-9]{3}[-_./]?)?[0-9]{3}[-_. /]?[0-9]{4}

************************************************************************

First group:

^([0-9]{3}[-_./]?)?

^ means the beginning of the string
[0-9] means a digit from 0 to 9
{3} means we want exaclty three of them
[-_./] is an optional grouping of dashes,underscore,periods, or slashes.
? These all mean zero or more.

So the first group reads:

"From the start line, find me 3 digits followed optionally by one or more dash,underscore,period, or slash."

Now the last ? says that this entire group is optional, so i we don't find anything at all, we can still match the next group.

************************************************************************

Second Group:

[0-9]{3}[-_. /]?

(Very similar to the first)
[0-9] means a digit from 0 to 9
{3} means we want exaclty three of them
[-_./] is an optional grouping of dashes,underscore,periods, or slashes.
? These all mean zero or more.

************************************************************************

Third Group:

[0-9]{4}
[0-9] means a digit from 0 to 9
{4} means we want exaclty four of them
0
 

Author Comment

by:markh51
ID: 8139666
Right,

So far so good, I've got:

<cfscript>
function IsAN(str) {
  if (REFindNoCase("[^a-z0-9][^-_]", str) eq 0)
    return true;
  else
    return false;
}
</cfscript>

Which I think means only accept a-z,0-9 and -,_ in the string ???

So how do I get it to accept spaces ?
0
 
LVL 8

Expert Comment

by:TallerMike
ID: 8139921
You can just hit the space bar and add a space.

I think you want to change your expression to be more like this:

^[a-zA-Z0-9-,_ ]$

And you can change your function to be like so:

<cfscript>
function IsAN(str) {
   return (REFindNoCase("^[a-zA-Z0-9-,_ ]$", str) eq 0);
}
</cfscript>

***************************************************************************

I read the following:

[^a-z0-9][^-_]

As:

Match a string that has something other than a-z or 0-9, followed by something other than a dash or an underscore.

When you use the caret (^) inside of brackets, it doesn't mean the beginning of the string anymore, it means NOT. So it needs to be at the beginning, and then a $ needs to be at the end to match the end of the string.

And by putting things in brackets, it means sortof a 'followed' by. So the following:

[d][o][g]

Would be any string that has an 'd' followed by a 'o' followed by a 'g' in it. The would also return true for 'doggy'. So we put the start and end characters like so:

^[d][o][g]$

And it will not match only 'dog'

Of course it would be easier to do this:

^(dog)$

But if we wanted to catch something like 'dogg', we'd need to seperate the characters in brackets so we could do this:

^(do)[g]{1,2}$
0
 

Author Comment

by:markh51
ID: 8140012
I'm sorry, but I must be thick or something as I can't get this to work. When I use your expression it will let me type in anything. What i need it to do is let you enter ONLY the following a-z,A-Z,0-9,_,- and spaces.

So for example you could type in:
TEST_USER
Test_User
test user
test-user
TEST-USER

but not "test#user" or "test<>user@;.." etc

Thanks
0
 
LVL 8

Accepted Solution

by:
TallerMike earned 200 total points
ID: 8140267
I tested the following expression with no problems:

^[a-zA-Z0-9-_ ]*$

Things I changed:

- I got rid of the comma, I thought you wanted it to there.
- I added an asterisk after the first group to signify zero or more of the previous (you could change this to a plus (+) to make it one or more)

Let me know how this goes...

Oh yeah, you'll also need to change your function like so:

<cfscript>
function IsAN(str) {
  return (REFindNoCase("^[a-zA-Z0-9-_ ]*$", str) eq 1);
}
</cfscript>

If the REFind returns 1, then it is true, not zero. Zero means it couldn't be matched. And since you're matching the beginning of the line, it will never return anything but 0 or 1.
0
 

Author Comment

by:markh51
ID: 8140316
Cheers mate, I finaly got it working. Thanks for all your help :-)
0

Featured Post

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question