1

I want to validate numbers in my form. The form should pass only with numbers starting with 5 6 7 8 and the length should be 8. This is my code.

<?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>';
}
?>
2
  • your code looks vulnerable to sql injection. use either PDO or mysqli but not mysql :) Commented Sep 4, 2015 at 17:03
  • thanks for the advice :) Commented Sep 4, 2015 at 17:15

1 Answer 1

3

If i understand right just want to validate numbers? Try this basic regex.

if (preg_match('/^[5-8][\d]{7}$/', $numero ))
{
    //DO OPERATIONS HERE...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.