有人知道我的正则表达式是正确的吗?

时间:2012-09-01 14:05:35

标签: php mysql regex submit

  

可能重复:
  Regular expression wont produce error echo, could anyone please help me?

当正则表达式不正确时,是否有人知道如何防止表单数据插入数据库。有没有办法用php禁用按钮(已经用javascript做了)。

 function ValidateForm($string, $type){

    switch($type)
        {
        case 'name':
            $pattern="/^[A-Za-z. -]+$/";
            break;
        case 'sex':
            $pattern="/[MWmw]/";
            break;
        case 'insertion':
            $pattern="/^[A-Za-z]*$/";
            break;
        case 'initials':
            $pattern="/^([A-Z]\.)+$/";
        break;
        case 'adress':
            $pattern="/^([A-Za-z -])+[0-9]+([a-z -])*$/";
            break;
        case 'postcode':
            $pattern="/^[1-9][0-9]{3}[]?[A-Za-z]{2}$/";
            break;
        case 'phone':
            $pattern="/^[0-9]{10}$/";
            break;
        case 'email':
            $pattern="/(^[0-9a-zA-Z_\.-]{1,}@([0-9a-zA-Z_\-]{1,}\.)+[0-9a-zA-Z_\-]{2,}$)/";
            break;
        case 'postbus':
            $pattern="/^[A-Z][a-zA-Z -]+$/";
            break;
        }



        if (preg_match($pattern, $string)){
                return true;
            } else {
                return false;
            }   
        }   



        if (ValidateForm($_POST["surname"],'name')){
            $error1 = false; 
        }else{
            $error1 = true; 
        } 

        if (ValidateForm($_POST["sex"],'sex')){
            $error2 = false; 
        }else{
            $error2 = true; 
        }





                if (ValidateForm($_POST["insertion"],'insertion')){
            $error3 = false;
        }else{
            $error3 = true; 
        }

        if (ValidateForm($_POST["initials"],'initials')){
            $error4 = false;
        }else{
            $error4 = true; 
        }

        if (ValidateForm($_POST["name"],'name')){
            $error5 = false;
        }else{
            $error5 = true; 
        }


        if (ValidateForm($_POST["adress"],'adress')){
            $error6 = false;
        }else{
            $error6 = true; 
        }

        if (ValidateForm($_POST["postcode"],'postcode')){
            $error7 = false;
        }else{
            $error7 = true; 
        }           

        if (ValidateForm($_POST["postbus"],'postbus')){
            $error8 = false;
        }else{
            $error8 = true; 
        }

        if (ValidateForm($_POST["private_phone"],'phone')){
            $error9 = false;
        }else{
            $error9 = true; 
        }

        if (ValidateForm($_POST["mobile_phone"],'phone')){
            $error10 = false;
        }else{
            $error10 = true; 
        }

        if (ValidateForm($_POST["work_phone"],'phone')){

            $error11 = false;

        }else{
            $error11 = true;
        }

        if (ValidateForm($_POST["work_email"],'email')){
            $error12 = false;
        }else{
            $error12 = true;
        }

        if (ValidateForm($_POST["private_email"],'email')){
            $error13 = false;
        }else{
            $error13 = true;
    }

1 个答案:

答案 0 :(得分:0)

简单,请改用存储过程。你只需要调用它们,让mysql核心为你工作。如果正则表达式匹配或不匹配,程序将返回一个布尔值。

使用mysql正则表达式REGEXP:http://dev.mysql.com/doc/refman/5.1/en/regexp.html

CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_REGEXP`(IN `Str` CHAR(32), OUT `result` INT)
        LANGUAGE SQL
        NOT DETERMINISTIC
        CONTAINS SQL
        SQL SECURITY DEFINER
        COMMENT ''
BEGIN
   DECLARE  result INT DEFAULT 0;
   SELECT str REGEXP '^[a-d]' INTO result;
END