简体   繁体   中英

How can i validate integer form in php?

I want to validate integer form using PHP. Do anyone of you know how to validate it? my code like this ` 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.

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:

HTML form

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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