-1

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?

Hoàng Trung Hiếu
  • 209
  • 1
  • 3
  • 14

3 Answers3

1

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.

Ali Zia
  • 3,825
  • 5
  • 29
  • 77
0

You can use either:

is_int

or

is_numeric
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
Arthur
  • 462
  • 3
  • 5
0

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.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100