PHP验证输入表单

时间:2012-11-15 21:25:42

标签: php validation function-parameter

我正在制作输入表单,目前我正在验证代码。我从博客中遇到这个验证码,看起来非常整洁。有人知道如何将这个代码实现到表单中吗?任何帮助表示赞赏。

function field_validator($field_descr, $field_data, $field_type, $min_length="", $max_length="", $field_required=1) {
    # array for storing error messages
    global $messages;

    # first, if no data and field is not required, just return now:
    if(!$field_data && !$field_required){ return; }
     # initialize a flag variable - used to flag whether data is valid or not
    $field_ok=false;
     # this is the regexp for email validation:
    $email_regexp="^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|"
    $email_regexp.="(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
     # a hash array of "types of data" pointing to "regexps" used to validate the data:
    $data_types=array(
        "email"=>$email_regexp,
        "digit"=>"^[0-9]$",
        "number"=>"^[0-9]+$",
        "alpha"=>"^[a-zA-Z]+$",
        "alpha_space"=>"^[a-zA-Z ]+$",
        "alphanumeric"=>"^[a-zA-Z0-9]+$",
        "alphanumeric_space"=>"^[a-zA-Z0-9 ]+$",
        "string"=>""
    );

    # check for required fields
    if ($field_required && empty($field_data)) {
        $messages[] = "$field_descr is a required field.";
        return;
    }

    # if field type is a string, no need to check regexp:
    if ($field_type == "string") {
        $field_ok = true;
    } else {
        # Check the field data against the regexp pattern:
        $field_ok = ereg($data_types[$field_type], $field_data);
    }

    # if field data is bad, add message:
    if (!$field_ok) {
        $messages[] = "Please enter a valid $field_descr.";
        return;
    }

    # field data min length checking:
    if ($field_ok && $min_length) {
        if (strlen($field_data) $max_length) {
            $messages[] = "$field_descr is invalid, it should be less than $max_length characters.";
            return;
        }
    }
} 

1 个答案:

答案 0 :(得分:2)

尝试使用Jquery ......

<form id="regForm" name="regForm" method="post" action="insert.php">
    <label for="Fname">Name </label>
    <input id="_Fname" name= "Fname" value=''/>
    <input id="_Lname" name= "Lname" value=''/>
    <input id="saveForm" type="submit" name="submit" value="Submit" />
</form>

js文件会是这样的......

(function($,W,D)
{
    var JQUERY4U = {};

    JQUERY4U.UTIL =
    {
        setupFormValidation: function()
        {
            //form validation rules
            $("#regForm").validate({
        rules: {
                    Fname:{ 
            required: true,
            minlength: 3
            },
                    Lname:{ 
            required: true,
            minlength: 3
            },
            },
            messages: {
                    Fname:{ 
            required: "Please enter your firstname",
            minlength: "name must b 3 character long"
            },
                    Lname: { 
            required: "Please enter your lastname",
            minlength: "name must b 3 character long"
            }
    }
    $(D).ready(function($) {
        JQUERY4U.UTIL.setupFormValidation();
     });
})(jQuery, window, document);