Jump to content

Numeric Validation of textbox


mikebyrne

Recommended Posts

I have two textboxes where I want the user to only be allowed to pass numeric values to my table. The textboxs are "Stockamount" & "Price". At present I've only validated for leaving the fields blank.

 

How do I code the numeric validation?

 

My code

 

if ($_POST['Stockamount']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_amount = "Please enter the ammount of stock recivied<br>";
									}
	if ($_POST['Price']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_price = "Please enter the product price<br>";
									}	

 

Link to comment
Share on other sites

So in my case?

 

if(!is_numeric($_POST['Price'])) {

  $valid=0;

                $style_name = "background-color:#FF5959";

                $error_amount = "Please enter numeric values only<br>";

    }

 

In terms of the Javascript side of things how would I code that around the textbox as Im not too familar with it?

 

The one bit i do have is around my clear button

 

<input type="reset" value="" style="border:0;background:url(../Admin_files/btn_clear.gif) no-repeat;width:73px;height:23px;" onclick="return confirm('Are you sure you want to clear all data?')">

 

The Price textbox looks like this

 

<input type="text" class="itemEditForm04" name="Price" value="<?php echo $row['Price'];?>" /> <td width="269"><font color="#FF0000" style="font-size: 8pt"><?php echo $error_price; ?></font></td>

Link to comment
Share on other sites

At present the validation still isnt working 100%

 

If I leave the textbox blank I get both error reports

If I insert only Letters = "Please enter a numeric value"

If I insert numbers = No errors (which is correct)

 

if ($_POST['Price']=="") {
                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_price = "Please enter the product price<br>";
				}

	if(!is_numeric($_POST['Price'])) {
   				$valid=0;
                $style_name = "background-color:#FF5959";
                $error_price1 = "Please enter numeric values only";
                                        }

<input type="text" class="itemEditForm04" name="Price" value="<?php $row['Price'];?>" /> <td width="269"><font color="#FF0000" style="font-size: 8pt"><?php echo $error_price; ?><?php echo $error_price1; ?></font></td>

 

Link to comment
Share on other sites

No need to for any complex regex

<?php

if(isset($_POST['Price']))
{
    if(empty($_POST['Price']))
    {
        $price_error = 'Please provide a price';
    }
    elseif(!is_numeric($_POST['Price']))
    {
        $price_error = 'Price is invalid. Numbers only (eg: 123.45)';
    }
}

?>
<input type="text" class="itemEditForm04" name="Price" value="<?php $row['Price'];?>" />
<?php if($price_error): ?><span style="font-size: 8pt; color:#FF0000"><?php echo $error_price; ?></span><?php endif; ?>

Link to comment
Share on other sites

See the problem I have is that my validation starts with

 

if($_POST["action"] == "Add"){

        $valid=1;

if ($_POST['Price']=="") {
                
                                $valid=0;
                $style_name = "background-color:#FF5959";
                $error_price = "Please enter the product price<br>";
									}	
	if(!is_numeric($_POST['Price'])) {
   				$valid=0;
                $style_name = "background-color:#FF5959";
                $error_price1 = "Please enter numeric values only";
                                        }

 

How can I combine the $valid with your example??

 

Link to comment
Share on other sites

Just add $valid=0; in the if/else statement:

if(isset($_POST['Price']))
{
    $valid = 1;
    if(empty($_POST['Price']))
    {
        $valid = 0;
        $price_error = 'Please provide a price';
    }
    elseif(!is_numeric($_POST['Price']))
    {
        $valid = 0;
        $price_error = 'Price is invalid. Numbers only (eg: 123.45)';
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.