This was a little learning curve for me. So thanks for letting me help you :D!
I'll start with the code:
[php]
<?php
$stringGood = "2345634545156456";
$stringBad = "32873428473t30984";
$pattern = '/[^0-9]/'; //pattern for regular expression
if(preg_match($pattern, $stringBad)){
echo "String: $stringBad, contained characters <em>other than</em> numbers.";
} else {
echo "String was good.";
}
echo "<br /><br />
if(preg_match($pattern, $stringGood)){
echo "String: <strong>$stringGood</strong>, contained characters <em>other than</em> numbers.";
} else {
echo "String was good.";
}
?>
[/php]
You better read this!
First we start off with the strings we are going to check ($stringGood, $stringBad) named like so for obvious reasons ;) - i assume you'll need to change the variables to something like:
[php]
$string = $_POST['number'];
[/php]
The next thing ($patter) is the pattern we will use for the regular expression, and don't be worried if these scare you - they scare me! But once you try it for yourself they aren't too ghastly.
Basically what the pattern is saying is:
[start pattern
^check for something that is NOT
0-9included in this part
]end pattern.
So we're checking for anything that is
not within the scope of 0, 1, 2...
Easy eh?
Next we're onto the
preg_match. What this does is check the given subject for any given regular expression. Great. Exactly what we need.
If the given expression is found in the given subject; it will return 1 / true. And i bet you can tell me what it returns if it isn't found? ;)
So so:
[php]
if(preg_match returns TRUE){
//pattern was found - execute code.
} else {
//pattern not found - execute code.
}
[/php]
Hope this long winded solution helps!
Any questions just post back :)
markus.