PHP 表单验证

1. 验证文本框是否有内容且不能为空

<?php
if (! (filter_has_var(INPUT_POST, 'flavor') && (strlen(filter_input(INPUT_POST, 'flavor')) > 0))) { print 'You must enter your favorite ice cream flavor.'; }

 

2. 验证字符串个数

<?php

if (! (filter_has_var(INPUT_POST, 'color') &&
    (strlen(filter_input(INPUT_POST, 'color', FILTER_SANITIZE_STRING)) <= 5))) {
    print 'Color must be more than 5 characters.';
}

 

3. 验证是否是数组

<?php

if (! (filter_has_var(INPUT_POST, 'choices') &&
    filter_input(INPUT_POST, 'choices', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY))) {
    print 'You must select some choices.';
}

 

4. 验证是否是整数

<?php

if (! (filter_has_var(INPUT_POST, 'age') &&
    filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT))) {
    print "Submitted age is invalid.";
}

 

5. 验证是否是浮点数

<?php

if (! (filter_has_var(INPUT_POST, 'price') &&
    filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT))) {
    print "Submitted age is invalid.";
}

 

posted @ 2017-01-05 22:15  ranwuer  阅读(163)  评论(0编辑  收藏  举报