Jahlil Espinoza
Jahlil Espinoza

Reputation: 83

html form number validation php

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

Upvotes: 1

Views: 157

Answers (1)

Varun
Varun

Reputation: 1946

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

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

Upvotes: 3

Related Questions