Number only string detection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjaywoods
Forum Newbie
Posts: 11
Joined: Wed Jul 12, 2006 9:32 am

Number only string detection

Post by mjaywoods »

Hi, I need to be able to detect if someone as not typed in numbers in a input field when its been submitted, can this be done?

I guess you'd something like

Code: Select all

<?php

// this would be $_POST[] etc not $date =

$date = "1";


if (ereg ("([0-9]{1,2})", $date, $regs)) {
   echo "$regs[1]";
} else {
   echo "Invalid date format: $date";
}
?>
but i can't get this to work any ideas??

thanks

Mjay
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

If you only need to verify that it is an integer you may want to use..

Code: Select all

if (!ctype_digit($_POST['variableName'])) {
    echo 'INVALID!';
}
mjaywoods
Forum Newbie
Posts: 11
Joined: Wed Jul 12, 2006 9:32 am

Thanks

Post by mjaywoods »

Thanks thats the thing i needed just couldn't find it, problem solved
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You could use is_numeric() too.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply