Jump to content

PHP script, that checks if


Super2

Recommended Posts

You can check if a variable contains only numbers in several ways:

[b]is_numeric()[/b] method
[code]
<?php

$foo = 20 // <== your variable

if( is_numeric($foo) ){
echo 'Variable is a number.';
} else {
echo 'Variable is NOT a number';
}

?>
[/code]

[b]Regular Expression[/b]
[code]
<?php

$foo = 20;

if( preg_match('/^[0-9]$/', $foo)){
echo 'Variable is a number.';
} else {
echo 'Variable is NOT a number';
}

?>
[/code]

I'm assuming that you'll eventually want to check other fields that may not be numbers, so I would recommend creating an validation / error handling class like the one mentioned at the link below:

http://www.phpfreaks.com/tutorials/117/0.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.