简体   繁体   English

我如何在 php 中验证整数形式?

[英]How can i validate integer form in php?

I want to validate integer form using PHP.我想使用 PHP 验证整数形式。 Do anyone of you know how to validate it?你们中有人知道如何验证它吗? my code like this ` Thông tin thành viên ">我的代码像这样` Thông tin thành viên ">

        <tr><td>First name : </td><td><input type="text" class="form-control" name="firstname"></td></tr>
        <tr><td>Last name : </td><td><input type="text" class="form-control" name="lastname" /></td></tr>
        <tr><td>User Name: </td><td><input type="text" class="form-control" name="username"/></td></tr>
        <tr><td>Password: </td><td><input type="password" class="form-control" name="password"></td></tr>
        <tr><td>Email: </td><td><input type="email" class="form-control" name="email"/></td></tr>
        <tr><td>Phone: </td><td><input type="text" class="form-control" name="phone"/></td></tr>
        <tr><td>Cấp độ : </td>
            <td>                
                <select class="form-control" name="level">
                    <option selected>---Free choose---</option>
                    <option value="1">Administrator</option>
                    <option value="0">Member</option>
                </select>
            </td>
        </tr>
        <tr>
        <td> <button type="submit" name="add" class="btn btn-primary btn-md">Thêm thành viên</button></td></tr>
    </table>`

When people input the phone i want to check whether it is integer or not?当人们输入电话时,我想检查它是否是整数? can you help?你能帮我吗?

Try using this is you want to check it on runtime.如果您想在运行时检查它,请尝试使用它。

<tr><td>Phone: </td><td><input type="number" class="form-control" name="phone"/></td></tr>

Or use is_int or is_numeric if you want to check the phone number after POST.或者如果您想在 POST 后检查电话号码,请使用is_intis_numeric

You can use either:您可以使用:

is_int

or或者

is_numeric

A safe attitude for the input validation process in php is like this for integer numbers: php 中输入验证过程的安全态度是这样的整数:

HTML form HTML 表单

<input type="text" name="phone"/>

PHP PHP

if( !filter_var( $_POST['phone'], FILTER_VALIDATE_INT) === false){
    echo("Variable is an integer");
}else{
    echo("Variable is not an integer");
}

Keep in mind that you can never trust the traffic originated from user, thus things like:请记住,您永远不能相信来自用户的流量,例如:

<input type="number" name="phone"/>

could not be the answer to this problem.不可能是这个问题的答案。 You may use such methods to just help the end users fill-in your forms.您可以使用此类方法来帮助最终用户填写您的表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM