DEVILofDARKNESS Posted August 12, 2009 Share Posted August 12, 2009 It's the first time I used regex, so before I launch it on the WWW, I want to know if it's well formed and safe. (It's an easy one, to check if the ammount of a textbox is a value above 0) preg_match("/^[^-]?[1-9]+/", $_POST['ammount'], $matches) Link to comment https://forums.phpfreaks.com/topic/169912-first-time-regex-is-this-right/ Share on other sites More sharing options...
thebadbad Posted August 12, 2009 Share Posted August 12, 2009 Your pattern currently tries to match an optional character NOT a hyphen (at the very start of the string) followed by 1 or more digits being 1 to 9 (e.g. 110 wouldn't match). And anything could follow after that (so you would want to use a dollar sign that matches at the end of the string). If you strictly want to check for an integer above 0, you can use ~^[1-9][0-9]*$~D But in that case you could obviously simply use built in functions: if (ctype_digit($_POST['ammount']) && ((int) $_POST['ammount'] > 0)) Link to comment https://forums.phpfreaks.com/topic/169912-first-time-regex-is-this-right/#findComment-896402 Share on other sites More sharing options...
DEVILofDARKNESS Posted August 12, 2009 Author Share Posted August 12, 2009 Thanks Link to comment https://forums.phpfreaks.com/topic/169912-first-time-regex-is-this-right/#findComment-896487 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.