Jump to content

is_numeric() function


guitarrgirl

Recommended Posts

I need to figure out how to use this function. I was given a problem where I have two columns of information that a user can enter into a form field. The first column is for text and the second is for only numbers. I need to create a validation so that when a user enters information into the form, then hits the submit button, the code should generate an error message if a number was not entered indicating  '"characters entered" is not a number.'

 

here is the code I have so far:

 

 

<html>

<head>

<title>My Bills</title>

</head>

<body style="font-family: Arial, Helvetica, sans-serif; color: #000000;">

<h1>My Bills</h1>

<form method=post>

<table>

<tr>

<th>Item</th>

<th>Amount</th>

</tr>

 

<?php

 

// Here is the Item code

for ($i = 1; $i < 5; $i++)

{

$item_name = 'item'."$i";

$item_value = $_POST[$item_name];

 

$amount_name = 'amount'."$i";

$amount_value = $_POST[$amount_name];

 

print '<tr>';

print "<td><input type=text name=$item_name value='$item_value'></td>\n";

print "<td><input type=text name=$amount_name value='$amount_value'></td>\n";

 

 

-------

 

The validation code need to go here, but I don't have a clue on how to use the function.

 

 

Any help would be greatly appreciated.

 

 

Email me offline... that is best...I'll get the message that way!

 

Thanks in advance,

 

 

:)

m

 

Link to comment
https://forums.phpfreaks.com/topic/70991-is_numeric-function/
Share on other sites

To take it one step further you could strip out any non-numerical characters in case there was a typo:

 

<?php

$letters = preg_split('//', $var, -1, PREG_SPLIT_NO_EMPTY);
foreach($letters as $key => $letter) if(!is_numeric($letter) && $letter != '.') unset($letters[$key);
if($letters) $checked_var = implode("", $letters);

if (is_numeric($checked_var)){
   echo "The value is numeric.";
} else {
   echo "ERROR: The value you entered is not numeric!";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/70991-is_numeric-function/#findComment-358502
Share on other sites

To take it one step further you could strip out any non-numerical characters in case there was a typo:

 

<?php

$letters = preg_split('//', $var, -1, PREG_SPLIT_NO_EMPTY);
foreach($letters as $key => $letter) if(!is_numeric($letter) && $letter != '.') unset($letters[$key);
if($letters) $checked_var = implode("", $letters);

if (is_numeric($checked_var)){
   echo "The value is numeric.";
} else {
   echo "ERROR: The value you entered is not numeric!";
}

?>

 

<?php
$var = preg_replace('/[^0-9]/', '', $var);
?>

 

Would be an easier way to remove all non-numeric characters from a string.

Link to comment
https://forums.phpfreaks.com/topic/70991-is_numeric-function/#findComment-358593
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.