Exclude norwegian characters and space
Moderator: General Moderators
Exclude norwegian characters and space
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.
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
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
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
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
Code: Select all
$subject = "æ-_";
$pattern = '#^[a-z0-9_]*$#i';
preg_match($pattern, $subject, $matches);
print_r($matches);
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
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Exclude norwegian characters and space
I find that hard to believe. å is not a part of the set [a-zA-Z0-9_].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.
What's wrong with "ærlige_øivind"? It seems to meet all your requirements.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.
Anyway, to solve this, you can simple add all the allowed characters to your existing set:
Code: Select all
'/^[a-zA-Z0-9_åæø]+$/'Good luck.
Re: Exclude norwegian characters and space
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.
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.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Exclude norwegian characters and space
I now just saw that you wanted to exclude the Norwegian characters... Silly me.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.
Note that the regex
Code: Select all
^[a-z0-9_]+$Code: Select all
^[a-zA-Z0-9_]+$