html表单号验证php

时间:2015-09-04 16:50:57

标签: php html forms validation

我想验证表单中的数字。表单应仅以5 6 7 8开头的数字传递,长度应为8.这是我的代码。

<?php
session_start();
$fecha=date("Y.m.d");
$numero = $_SESSION["numero"];
if(filter_var($numero, strlen($numero ==8),FILTER_VALIDATE_INT) AND in_array(substr($numero, 0, 1), array(5,6, 7, 8))) {
    $conexion = mysql_connect("server","user","pass") or die (mysql_error()); mysql_select_db('db',$conexion) or die(mysql_error());
    $result = mysql_query("INSERT INTO `db`.`Incoming` (ID ,MsgExternalID ,MsgFrom ,MsgTo ,MsgBody ,MsgBodyType ,MsgTag ,MsgPriority ,MsgDLRRequested ,MsgOriginatedTime ,MsgScheduledTime ,MsgExpiringTime) VALUES (NULL ,  '0', '+506$numero', '+8384', '', '0', '', NULL , NULL , NULL , NULL , NULL)", $conexion); 
    if($result!=NULL)
    {
        header('Location: page.php');
    }
}else{
    echo '<script language="javascript">';
    echo 'alert("Ingrese un número valido");';
    echo 'history.back()';
    echo '</script>';
}
?>

1 个答案:

答案 0 :(得分:3)

如果我理解正确只是想验证数字? 试试这个基本的正则表达式。

if (preg_match('/^[5-8][\d]{7}$/', $numero ))
{
    //DO OPERATIONS HERE...
}