我使用PHP filter_validate_int
执行简单的电话验证。长度应该是10个字符,所有都应该是数字。但是,由于大多数电话号码都以0开头。过滤器验证int函数返回false。无论如何都要解决这个问题。这是我用过的代码
if(!filter_var($value, FILTER_VALIDATE_INT) || strlen($value) != 10) return false;
答案 0 :(得分:6)
您无法做任何事情来使此验证工作。在任何情况下,您都不应该使用FILTER_VALIDATE_INT
,因为电话号码不是整数;它们是数字字符串。
如果你想确保$tel
是一个由10个数字组成的字符串,你可以使用正则表达式:
if (preg_match('/^\d{10}$/', $tel)) // it's valid
或(或许更好)一些oldschool字符串函数:
if (strlen($tel) == 10 && ctype_digit($tel)) // it's valid
答案 1 :(得分:1)
您可以使用正则表达式:
if (!preg_match('~^\d{10}$~', $value)) return false;
答案 2 :(得分:1)
使用preg_match
$str = '0123456789';
if(preg_match('/^\d{10}$/', $str))
{
echo "valid
}
else
{
echo "invalid";
}
答案 3 :(得分:0)
0不是int的有效起始编号。 0是八进制数的起始数。
电话号码是字符串,而不是int。您必须使用正则表达式来验证它。
答案 4 :(得分:0)
可能需要检查返回是否为0。过滤器在验证成功时返回输入,或者在失败时返回false。
使用严格比较(===或!==)进行比较,例如$ result!== false。
if(filter_var($squid, FILTER_VALIDATE_INT)!==false ) {
//if it's here, it passed validation
}
您还可以使用is_numeric函数: http://php.net/manual/en/function.is-numeric.php
答案 5 :(得分:0)
这是一个PHP错误-#43372
正则表达式很好,但是会消耗一些资源。
此方法适用于任何整数,包括零和前导零
{
~~~
"organizations": {
"bankema": {
"mspid": "bankemaMSP",
"peers": {
"peer0.bankema-net": {}
},
"certificateAuthorities": {
"ca.bankema-net": {}
},
"fullpath": true,
"adminPrivateKey": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/bankema-net/msp/keystore/server.key"
},
"signedCert": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/bankema-net/msp/signcerts/server.crt"
}
},
"vendor": {
"mspid": "vendorMSP",
"peers": {
"peer0.vendor-net": {}
},
"certificateAuthorities": {
"ca.vendor-net": {}
},
"fullpath": true,
"adminPrivateKey": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/vendor-net/msp/keystore/server.key"
},
"signedCert": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/vendor-net/msp/signcerts/server.crt"
}
},
"factor": {
"mspid": "factorMSP",
"peers": {
"peer0.factor-net": {}
},
"certificateAuthorities": {
"ca.factor-net": {}
},
"fullpath": true,
"adminPrivateKey": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/factor-net/msp/keystore/server.key"
},
"signedCert": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/factor-net/msp/signcerts/server.crt"
}
},
"corg": {
"mspid": "corgMSP",
"peers": {
"peer0.corg-net": {}
},
"certificateAuthorities": {
"ca.corg-net": {}
},
"fullpath": true,
"adminPrivateKey": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/corg-net/msp/keystore/server.key"
},
"signedCert": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/corg-net/msp/signcerts/server.crt"
}
},
"insurer": {
"mspid": "insurerMSP",
"peers": {
"peer0.insurer-net": {}
},
"certificateAuthorities": {
"ca.insurer-net": {}
},
"fullpath": true,
"adminPrivateKey": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/insurer-net/msp/keystore/server.key"
},
"signedCert": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/insurer-net/msp/signcerts/server.crt"
}
},
"investor3": {
"mspid": "investor3MSP",
"peers": {
"peer0.investor3-net": {}
},
"certificateAuthorities": {
"ca.investor3-net": {}
},
"fullpath": true,
"adminPrivateKey": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/investor3-net/msp/keystore/server.key"
},
"signedCert": {
"path": "/mnt/fabric/crypto-config/peerOrganizations/investor3-net/msp/signcerts/server.crt"
}
}
}
~~~
}