InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Validating Form Input: Numbers in PHP
PHP

Validating Form Input: Numbers in PHP

InfinityCoder November 28, 2016

You want to make sure a number is entered in a form input box. For example, you don’t want someone to be able to say that her age is old enough or tangerine, but instead want values such as 13 or 56.

If you’re looking for an integer, use the FILTER_VALIDATE_INT filter, as shown in Example 9-6.
Example 9-6. Validating a number with FILTER_VALIDATE_INT

1
2
3
4
$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT);
if ($age === false) {
   print "Submitted age is invalid.";
}

If you’re looking for a decimal number, use the FILTER_VALIDATE_FLOAT filter, as shown in Example 9-7.
Example 9-7. Validating a number with FILTER_VALIDATE_FLOAT

1
2
3
4
$price = filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT);
if ($price === false) {
   print "Submitted price is invalid.";
}

The FILTER_VALIDATE_INT and FILTER_VALIDATE_FLOAT filters cause filter_input() to return a number of the specified type (int or float) if the input string represents an appropriate number for the filter, or false otherwise.
There are a few filter flags that affect these number filters. The FILTER_FLAG_ALLOW_OCTAL flag tells FILTER_VALIDATE_INT to accept octal notation.

That is, a submitted string of 017 will cause the integer 15 to be returned. Similarly, the flag FILTER_FLAG_AL
LOW_HEX allows a submitted string of 0x2f to be returned as the integer 47.
The FILTER_FLAG_ALLOW_THOUSAND modifies the behavior of the FILTER_VALIDATE_FLOAT filter by allowing commas as a thousands separator.

Without it, 5,252 will be considered invalid. With it, 5,252 correctly validates as the float 5252.
If you’re a fan of regular expressions, those can be useful in certain validation situations.
Example 9-8 shows regular expressions that validate an integer and a decimal number.
Example 9-8. Validating numbers with regular expressions

1
2
3
4
5
6
7
8
9
10
11
12
13
// The pattern matches an optional—sign and then
// at least one digit
if (! preg_match('/^-?\d+$/',$_POST['rating'])) {
    print 'Your rating must be an integer.';
}
 
// The pattern matches an optional—sign and then
// optional digits to go before a decimal point
// an optional decimal point
// and then at least one digit
if (! preg_match('/^-?\d*\.?\d+$/',$_POST['temperature'])) {
   print 'Your temperature must be a number.';
}

It is a common refrain among performance-tuning purists that regular expressions should be avoided because they are comparatively slow.

In this case, however, with such simple regular expressions, they are about equally efficient as the filter functions.

If you’re more comfortable with regular expressions, or you’re using them in other validation contexts as well, they can be a handy choice.

The regular expression also allows you to consider valid numbers, such as 782364.238723123, that cannot be stored as a PHP float without losing precision.

This can be useful with data such as a longitude or latitude that you plan to store as a string.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Iterating Efficiently over Large or Expensive Datasets in PHP
You want to iterate through a list of items, but …

Iterating Efficiently over Large or Expensive Datasets in PHP

Returning Values by Reference in PHP
You want to return a value by reference, not by …

Returning Values by Reference in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap