2015-09-04 69 views
1

我想驗證我的表單中的數字。表格只能通過以5 6 7 8開始的數字,長度應爲8.這是我的代碼。html表單數字驗證php

<?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>'; 
} 
?> 
+0

你的代碼看起來很容易受到sql注入的影響。使用PDO或mysqli但不是mysql :) – aimme

+0

感謝您的意見:) –

回答

4

如果我理解的權利只是想驗證數字? 試試這個基本的正則表達式。

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

非常感謝你這個代碼工作完美。 –