It's pretty easy. Note that the error checking for all chars and num needs to be something like:
if(!ereg("[0-9a-zA-Z]+$", $_POST['personnr']))
Note that the ^ at the left and the $ at the right in the first arg anchor the regex to the two ends of the string being compared. I.e.
"[0-9Z-Aa-z]" Tells ereg to match any string that has at least one of these characters. It could easily have other characters like @ in there two, since "A@" and "A" both have a letter in them.
"[0-9]"
Tells the regular expression engine to match on a string that begins with a number
"[A-Z]$"
tells the regex engine to match any string ending with a capitol letter.
Now, when we want to make sure that we have a string that is anchored on both ends by the regex we're using, we add those two together, but something odd happens if we don't add a * or a +, like this:
<?php
$a="aa";
print ereg("^[a-z]",$a)."\n";
print ereg("[a-z]$",$a)."\n";
print ereg("^[a-z]$",$a)."\n";
?>
Note that only the first two show true. The third one is false because we are literally saying, match a string that starts with a single lowercase letter, and ends in the exact same letter. I.e. [a-z] matches only one character. We want it to match one OR MORE characters.
We could use or + to do this. means 0 or more times, while + means 1 or more times. Note the strange behaviour of * here:
<?php
$a="aa#";
print ereg("^[a-z]$*",$a);
?>
Note that this returns true, and it technically is true, because * means ZERO or more, and sure enough, that pattern occurs zero times.
Now we can try this:
<?php
$a="aa#";
print ereg("^[a-z]$+",$a); # should fail
$a="aa2";
print ereg("^[a-z]$+",$a); # should pass
More to come...