Preg_match 0-9验证 - 不接受零(0)

时间:2017-03-07 03:17:51

标签: php regex preg-match

我正在努力验证电话号码和分机号码,我需要做的是,如果没有可用的分机号码,我应该只放零(0)。我的问题是字段不接受数字“0”。

我该如何解决?

这是我用过的表达方式。

preg_match("/^[0-9]*$/", $ext)

<?php

$Errext="";
if(isset($_POST["submit"])){

if (empty($_POST["ext"])) {
            $Errext = "Please enter your extension #. If you do not have an extension # then enter zero(0).";
        } else {
            $ext = test_input($_POST["ext"]);
            if (!preg_match("/^[0-9]*$/", $ext)) {
                $Errext = "Only Numbers is allowed";
            } else {
                $extSuc = 1;
            }
        }  
}

function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }


?>
<form method="POST" class="form-horizontal" action="" id="form1">
<div class="col-md-offset-1 col-md-5">
                            <div class="form-group">
                                <label for="ext">Ext</label> 
                                <input class="form-control" name="ext" type="number" id="ext" value="<?php echo $ext; ?>"/>
                                <span>
                                    <?php echo $Errext; ?>
                                </span>
                            </div>
                        </div>

    <button name="submit" value="Submit" class="btn btn-primary col-md-offset-10 col-md-2">Submit</button>
</form>

你可以在这里运行代码

http://phpfiddle.org/

感谢。

我刚刚解决了这个问题。

if ($_POST["ext"]=='') {

抱歉遇到大麻烦,非常感谢你的帮助。

4 个答案:

答案 0 :(得分:1)

你永远不会输入你的正则表达式。 0empty

if (empty($_POST["ext"])) {
  

如果var存在并且具有非空的非零值,则返回FALSE。否则返回TRUE。

试试if (isset($_POST["ext"])) {。然后你也可以使用

http://php.net/manual/en/function.ctype-digit.phphttp://php.net/manual/en/function.is-numeric.php验证字符串是否为数字...或者您可以更新正则表达式以要求存在数字。 *允许字符串为空,使其成为+并且必须存在值。

^[0-9]+$

答案 1 :(得分:0)

这不接受零:

preg_match("/^[1-9]*$/", $ext)

答案 2 :(得分:0)

那是你想要的......

var_dump(preg_match("/^[1-9]*$/", '0987777777')); // return 0;

答案 3 :(得分:0)

只需要删除该0并用最小范围替换它。 #include<stdio.h> #include<stdlib.h> #include<errno.h> #include<fcntl.h> #include<termios.h> #include<unistd.h> #define SERIAL_PORT_PATH "/dev/ttyACM0" struct termios SerialPortSettings; unsigned char Buffer[256] = {0X01,0X04,0X00,0X4B,0x01,0XB1}; int Serial_Port_Descriptor = 0; int Open_flag = O_RDWR | O_NOCTTY | O_NDELAY; int Set_Nonblock_Open_flag(); int Serial_Port_Configuring(); int Read_Serial_Port(); int main() { Serial_Port_Descriptor = open(SERIAL_PORT_PATH,Open_flag); if(Serial_Port_Descriptor > 0) { printf("Serial_Port has been Opened \n"); Set_Nonblock_Open_flag(); } else { perror("Error:"); } Serial_Port_Configuring(); write(Serial_Port_Descriptor,&Buffer,sizeof(Buffer)); sleep(2); while(1) { Read_Serial_Port(); } close(Serial_Port_Descriptor); return 0; } int Set_Nonblock_Open_flag() { Open_flag = fcntl(Serial_Port_Descriptor,F_GETFL,Open_flag); if(Open_flag == -1) { return -1; } else if (0) { Open_flag |= O_NONBLOCK; printf("The Flag is set to Nonblock \n"); } else { Open_flag &= ~O_NONBLOCK; } return fcntl(Serial_Port_Descriptor,F_SETFL,Open_flag); } int Serial_Port_Configuring() { tcgetattr(Serial_Port_Descriptor,&SerialPortSettings); cfsetispeed(&SerialPortSettings,B115200); cfsetospeed(&SerialPortSettings,B115200); SerialPortSettings.c_cflag &= ~PARENB; SerialPortSettings.c_cflag &= ~CSTOPB; SerialPortSettings.c_cflag &= ~CSIZE; SerialPortSettings.c_cflag |= CS8; SerialPortSettings.c_cflag &= ~CRTSCTS; SerialPortSettings.c_cflag |= CREAD | CLOCAL; SerialPortSettings.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IXANY); SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHONL | ISIG | IEXTEN); SerialPortSettings.c_oflag &= ~OPOST; /* No Ouptut Processing */ SerialPortSettings.c_cc[VMIN] = 1; /* Read atleast 1 character */ SerialPortSettings.c_cc[VTIME] = 0; /* Wait Indefinitely */ tcflush(Serial_Port_Descriptor,TCIOFLUSH); if(tcsetattr(Serial_Port_Descriptor,TCSANOW,&SerialPortSettings) != 0) { printf("Error: The Settings are not been Configured \n"); return -1; } else { printf("Settings have been Configured \n"); } return 0; } int Read_Serial_Port() { int Serial_Port_Read = read(Serial_Port_Descriptor,&Buffer,sizeof(Buffer)); if(Serial_Port_Read < 0) { fputs("Read of bytes() has failed \n",stderr); return -1; } else if(Serial_Port_Read > 0) { printf("%d bytes has been read \n",Serial_Port_Read); for(int i=0;i <= Serial_Port_Read;i++) { printf("%02X",(unsigned char)Buffer[i]); } } return 0; }