Page 1 of 1
Exclude norwegian characters and space
Posted: Tue May 05, 2009 2:38 am
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.
Re: Exclude norwegian characters and space
Posted: Tue May 05, 2009 2:50 am
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);
Re: Exclude norwegian characters and space
Posted: Tue May 05, 2009 3:55 am
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

Re: Exclude norwegian characters and space
Posted: Tue May 05, 2009 3:59 am
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
Re: Exclude norwegian characters and space
Posted: Wed May 06, 2009 2:08 am
by Benjamin

Moved to Regex
Re: Exclude norwegian characters and space
Posted: Wed May 06, 2009 2:52 am
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:
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.
Re: Exclude norwegian characters and space
Posted: Thu May 07, 2009 4:56 am
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.
Re: Exclude norwegian characters and space
Posted: Thu May 07, 2009 5:12 am
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
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:
Good luck.