Php 仅数字验证功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10551551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:26:50  来源:igfitidea点击:

Php only numbers Validating function

phpvalidation

提问by PhilMarc

I would like to create a validate function for numbers only, actually I have those ones that works fine, and I tried to create one myself, but it's unfortunately not working. Here are the alphanumeric and others working fine :

我只想为数字创建一个验证函数,实际上我有那些工作正常的函数,我尝试自己创建一个,但不幸的是它不起作用。这是字母数字和其他工作正常的:

// Validators
private function custom($validator, $value)
{
    return call_user_func($validator, $value, $this->request, $this->id);
}

private function alphanumeric($value, $custom)
{
    return preg_match('/^(['.$custom.'a-z0-9_]*)$/i', $value);
}

private function valid_email($value)
{
    return preg_match('/^\S+@\S+\.\S+$/', $value);
}

And the one I tried to create by modifying the alphanumeric one :

我试图通过修改字母数字来创建一个:

private function numbers_only($value, $custom)
{
    return preg_match('/^(['.$custom.'0-9_]*)$/i', $value);
}

What's wrong with this one ?

这个有什么问题吗?

EDIT : I also have a JS helping with the form, for alphanumeric it's :

编辑:我还有一个 JS 帮助处理表单,对于字母数字,它是:

Form.prototype.alphanumeric = function(value, custom)
{
    return !value.replace(new RegExp('['+custom+'a-z0-9_]', 'ig'), '').length;
};

What would be the JS for numeric only ?

只有数字的 JS 是什么?

回答by Johannes Klau?

Use

is_numeric($value);

return is true or false

返回是真还是假

回答by Michael Berkowski

If you want only numbers, remove the $custompart from the function. The /iimplies case-insensitive matching, which is not relevant for numeric matches, and so can be removed.

如果您只需要数字,请$custom从函数中删除该部分。这/i意味着不区分大小写的匹配,这与数字匹配无关,因此可以删除。

private function numbers_only($value)
{
    return preg_match('/^([0-9]*)$/', $value);
}

The expression above will match zero or more numbers, so blank input is allowed. To require at least one number, change *to +as in

上面的表达式将匹配零个或多个数字,因此允许空白输入。至少需要一个号码,改变*+

return preg_match('/^([0-9]+)$/', $value);

And the [0-9]+can be abbreviated as \d+. Since you are not capturing the value inside a an array of matches, there is no need for the extra overhead which is added by including the ()capture group. That can be omitted as well.

并且[0-9]+可以缩写为\d+. 由于您没有捕获匹配数组中的值,因此不需要通过包含()捕获组而增加的额外开销。这也可以省略。

return preg_match('/^\d+$/', $value);

Or skip the regex entirely...

或者完全跳过正则表达式......

Finally, if you've gotten this far and are matching only integers, it is far easier and less resource-intensive to just do:

最后,如果你已经做到了这一点并且只匹配整数,那么这样做会更容易且资源占用更少:

// If you really intend to match numbers only, and not all numeric values
// which might include .,
function numbers_only($value)
{
  return ctype_digit(strval($value));
}

回答by Zauker

In PHP, for a "only numbers" validation you can use different approaches:

在 PHP 中,对于“仅数字”验证,您可以使用不同的方法:

  • is_int or is_integer
  • is_numeric
  • regular expressions
  • ctype_digit
  • filter_var
  • is_int 或 is_integer
  • is_numeric
  • 常用表达
  • ctype_digit
  • filter_var

is_integer()

is_integer()

for this function these values are are not valid: "0010", "123"

对于此功能,这些值无效:“0010”、“123”

is_numeric()

is_numeric()

for this function these values are valid: 1.3, +1234e44 and 0x539

对于这个函数,这些值是有效的:1.3、+1234e44 和 0x539

filter_var()

filter_var()

for this function a value as "00123" is not valid

对于此函数,值“00123”无效

CONSLUSION

结论

it seems that only regexand ctype_digitwork always fine.

似乎只有regexctype_digit 才能正常工作。

TEST

测试

a simple test here

一个简单的测试在这里