• Status: Solved
• Priority: Medium
• Security: Public
• Views: 172

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

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 ??

0
markh51
• 6
• 4
• 2
1 Solution

Commented:
0

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

Commented:
either do I but if i needed to i prob would good luck
0

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

Author Commented:
Oppps, I pressed refresh too many times
0

Commented:
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.

"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 Commented:
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

Commented:
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>

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

[^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 Commented:
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

Commented:
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 Commented:
Cheers mate, I finaly got it working. Thanks for all your help :-)
0

## Featured Post

• 6
• 4
• 2
Tackle projects and never again get stuck behind a technical roadblock.