Exclude norwegian characters and space

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
gunnar
Forum Newbie
Posts: 3
Joined: Tue May 05, 2009 2:24 am

Exclude norwegian characters and space

Post by gunnar »

I need a regex that matches a string containing only the letters of the english alphabet, upper and lower case, all digits and underscore.

I'm trying to validate a input form with Dreamweaver, programming in ASP.

I have tried to use the standard [a-zA-Z0-9_] regex. When I enter the character å (which is equivalent with the å) my regex works.

But when I add one more character to my input, i.e. "år", then my regex doesn't work.

I need my regex to accept inputs like these: "123_potet", "retur_45_a", "UNIFORM_79a"

It should NOT accept inputs like these: "are you there", "hello-world", "årlig hendelse", "24 år", "ærlige_øivind"

Please advice.
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Exclude norwegian characters and space

Post by papa »

You can probably do it a bit neater, but it works for the specs you gave.

Code: Select all

$subject = "ærlige_øivind";
$pattern = '#^[a-z0-9_]*$#i';
 
preg_match($pattern, $subject, $matches);
print_r($matches);
gunnar
Forum Newbie
Posts: 3
Joined: Tue May 05, 2009 2:24 am

Re: Exclude norwegian characters and space

Post by gunnar »

Thanks for the quick reply, but the code you gave didn't do the trick.

Instead of using MY regex [a-zA-Z0-9_], I tried yours #^[a-z0-9_]*$#i

Then I enter the following into my text field on the form: æ
This triggers the validation with the response: Please enter a valid value.

When I enter the following into my text field on the form: æf
This is approved by my validation regex.

The same happens if I try to enter the following into my text field: æ-_

It seems that the validation regex works when there is only 1 character in my input string.
Can you please tell me what is wrong with my regex?

I see from your code that you have put the character ' at the front and at the end of your regex.
Should I include this into my regex or not??

As you probably understand, I'm a bit of a novice in this area :-)
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Exclude norwegian characters and space

Post by papa »

Code: Select all

 
$subject = "æ-_";
$pattern = '#^[a-z0-9_]*$#i';
 
preg_match($pattern, $subject, $matches);
print_r($matches);
 
This doesn't give me a match.

You can always add $subject= trim($subject);

What does your code look like?

The ^ means start of string, $ means end of string.

I like Chris's tutorial: viewtopic.php?t=33147
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Exclude norwegian characters and space

Post by Benjamin »

:arrow: Moved to Regex
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Exclude norwegian characters and space

Post by prometheuzz »

gunnar wrote:I need a regex that matches a string containing only the letters of the english alphabet, upper and lower case, all digits and underscore.

I'm trying to validate a input form with Dreamweaver, programming in ASP.

I have tried to use the standard [a-zA-Z0-9_] regex. When I enter the character å (which is equivalent with the å) my regex works.

But when I add one more character to my input, i.e. "år", then my regex doesn't work.
I find that hard to believe. å is not a part of the set [a-zA-Z0-9_].
gunnar wrote:I need my regex to accept inputs like these: "123_potet", "retur_45_a", "UNIFORM_79a"

It should NOT accept inputs like these: "are you there", "hello-world", "årlig hendelse", "24 år", "ærlige_øivind"

Please advice.
What's wrong with "ærlige_øivind"? It seems to meet all your requirements.

Anyway, to solve this, you can simple add all the allowed characters to your existing set:

Code: Select all

'/^[a-zA-Z0-9_åæø]+$/'
And as already mentioned before me: you need to "anchor" the start- and end of the string. Otherwise, the preg_match function will return true when just a character from your set is present instead of all character.
Good luck.
gunnar
Forum Newbie
Posts: 3
Joined: Tue May 05, 2009 2:24 am

Re: Exclude norwegian characters and space

Post by gunnar »

Hi there.

As I'm using DreamWeaver and Validate Form extension, I realized I didn't needed the
'#^[a-z0-9_]*$#i' regex.

I trimmed down the code to
^[a-z0-9_]+$

and now everything works like a charm.

Thanks for all the good advises - I've learned a lot these days.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: Exclude norwegian characters and space

Post by prometheuzz »

gunnar wrote:Hi there.

As I'm using DreamWeaver and Validate Form extension, I realized I didn't needed the
'#^[a-z0-9_]*$#i' regex.

I trimmed down the code to
^[a-z0-9_]+$

and now everything works like a charm.

Thanks for all the good advises - I've learned a lot these days.
I now just saw that you wanted to exclude the Norwegian characters... Silly me.

Note that the regex

Code: Select all

^[a-z0-9_]+$
does not match upper case letters, so I'm assuming you enabled some sort of case-insensitive-matching inside Dreamweaver. If not, your regex should look like this:

Code: Select all

^[a-zA-Z0-9_]+$
Good luck.
Post Reply