Show The Note that the regular expression you've given also isn't a perfect integer checker, the way you've written it. It doesn't allow for negatives; it does allow for a zero-length string (ie with no digits at all, which presumably shouldn't be valid?), and it allows the number to have any number of leading zeros, which again may not be the intended. [EDIT] As per your comment, a better regular expression might look like this:
This forces the first digit to only be between 1 and 9, so you can't have leading zeros. It also forces it to be at least one digit long, so solves the zero-length string issue. You're not worried about negatives, so that's not an issue. You might want to restrict the number of digits, because as things stand, it will allow strings that are too big to be stored as integers. To restrict this, you would change the star into a length restriction like so:
This would allow the string to be between 1 and 16 digits long (ie the first digit plus 0-15 further digits). Feel free to adjust the numbers in the curly braces to suit your own needs. If you want a fixed length string, then you only need to specify one number in the braces. Hope that helps. In this article, we will go over how to perform number, or numeric, validation of a form field filled in by a user, to make sure that the data entered is, in fact, a numeric value. This may be necessary for a slew of forms filled out on the web. Examples of these are credit card numbers, zip codes, house numbers, telephone numbers, cash amounts, etc. These are all form fields where we want only numbers entered in to the form field. If any characters in the sequence are non-numeric, then we want to tell the user that the sequence of characters which they entered in are not valid. This is what is referred to as number validation. Below is an example of a form field which checks for number validation. Only numbers can be entered in to the form field, only numeric values. If non-numeric values are entered, then the form field catches this and tells the user that s/he has made an error. ResultAbove is a form which checks for number validation. In this form, it is wanted that users enter numbers only into the form field. If a user enters a nonnumeric character, an error is thrown and the user is told to enter numbers only into the field. PHPSo how do we perform number validation in PHP on a form field to make sure only numbers have been entered? The answer is, we use a function in PHP called the is_numeric function. The is_numeric function is used to check whether the character(s) which are entered. If all the characters are numeric, it returns a true value. If all the characters are not numeric, it returns a false, or not true, value. Knowing now how this function works, we can implement it based on an if-else statement. If true, we can make it execute one statement. If false, we can make it execute another statement, such as the form above does. HTML CodeThis HTML code creates the text field in which a user enters characters. The important thing we need to know from this HTML Code is the "name" attribute of the text field, which in this case is number. We will need to know this for the PHP code, because the "name" attribute is what we use in PHP of the HTML form field to test the characters that the user enters into this field. PHP Code(PHP 4, PHP 5, PHP 7, PHP 8) is_numeric — Finds whether a variable is a number or a numeric string Descriptionis_numeric(mixed Parametersvalue The variable being evaluated. Return Values Returns Changelog
ExamplesExample #1 is_numeric() examples
$tests as $element) { The above example will output: '42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array ( ) is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric Example #2 is_numeric() with whitespace
$tests as $element) { Output of the above example in PHP 8: ' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric Output of the above example in PHP 7: ' 42' is numeric '42 ' is NOT numeric ' 9001' is NOT numeric '9001 ' is NOT numeric See Also
sobolanx at gmail dot com ¶ 11 years ago
moskalyuk at gmail dot com ¶ 16 years ago
ben at chico dot com ¶ 8 years ago
kouber at saparev dot com ¶ 18 years ago
Magnus Deininger, dma05 at web dot de ¶ 13 years ago
How do I allow only numbers in PHP?In PHP, for a "only numbers" validation you can use different approaches:. is_int or is_integer.. is_numeric.. regular expressions.. ctype_digit.. filter_var.. What is not numeric in PHP?The is_numeric() function is used to check whether a variable is numeric or not. *Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types. Return value: TRUE if var_name is a number or a numeric string, FALSE otherwise.
Is numeric array PHP?Numeric arrays allow us to store multiple values of the same data type in a single variable without having to create separate variables for each value. These values can then be accessed using an index which in case of numeric arrays is always a number. Note: By default the index always starts at zero.
What is the use of Preg_match in PHP?PHP | preg_match() Function. This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.
|