Jump to content

How will i check whether the characters i am entering is numeric


bindiya

Recommended Posts

I have a text firld to enter amount af a book,But i need to check whether the data entering is numeric only eg:70.98 etc.

how can i achieve this.

 

my code is

echo"<table><tr><td></td><td>Amount  in USD</td><td></td><td><input type='text' name='amnt' id='amnt'  /> </td><td></td></tr></table>";

 

bindiya,

 

Assuming that your code is as follows:

 

echo"<table><tr><td></td><td>Amount  in USD</td><td></td><td><input type='text' name='amnt' id='amnt'  /> </td><td></td></tr></table>";

 

what are you trying to do?

 

Are you trying to check whether amnt is a number?

 

What you should be doing is as follows:

 

$amnt = 10;  //just a random number

echo"<table><tr><td></td><td>Amount  in USD</td><td></td><td><input type='text' name='" . $amnt . "' id='" . $amnt . "'  /> </td><td></td></tr></table>";

 

Then you can check the amnt variable to see whether it is numeric, and create an if statement.  For example:

 

if (isnumeric($amnt))

  {

  echo"<table><tr><td></td><td>Amount  in USD</td><td></td><td><input type='text' name='" . $amnt . "' id='" . $amnt . "'  /> </td><td></td></tr></table>";

  }

else

  {

  echo"<table><tr><td>This product has no price associated with it.</td></tr>

  }

 

Ofcourse, this means that your variable has to become a php variable.

 

still i get error  iam pasting my old code.to this code i need to print the message if the amount is not entered or the is not in nmbers.

 

 

echo "<table width='599' align='center'><tr><td></td><td>Username</td><td></td><td>  $username</td><td></td></tr>".

"<tr><td></td><td>Name</td><td></td><td> $fname</td><td></td></tr>".

"<tr><td></td><td>Email</td><td></td><td>  $email</td><td></td></tr>".

"<tr><td></td><td>Company Name</td><td></td><td> $companyname</td><td></td></tr>".

"<tr><td></td><td>Address</td><td></td><td>$address</td><td></td></tr>".

"<tr><td></td><td>State</td><td></td><td>  $state</td><td></td></tr>".

"<tr><td></td><td>Country</td><td></td><td>  $country</td><td></td></tr>".

"<tr><td></td><td>Website</td><td></td><td> $website</td><td></td></tr>".

"<tr><td></td><td>Description Of Payment</td><td></td><td> $pay_desc</td><td></td></tr>".

"<tr><td></td><td>Amount  in USD</td><td></td><td><input type='text' name='amnt' id='amnt'  /> </td><td></td></tr>";

 

 

}

 

 

yes i tested with a js function as follows

still not working

 

code pasted below

<script language="javascript">

  function verify(){

  var amnt=document.getElementById(amnt).value;

  if(document.getElementById(amnt).value==''){

 

  alert ("Please enter the amount");

  amnt.focus();

  return false;

  }

  else if(isNaN(amnt){

  alert('Amount should be in number');

  return false;

  }else{

  return true;

  }

 

 

  }

 

 

  </script>

                         

<form name='checkoutform' action='paynow.php' method='post'  >

 

 

<?php

  echo "<table width='599' align='center'><tr><td></td><td>Username</td><td></td><td>  $username</td><td></td></tr>".

"<tr><td></td><td>Name</td><td></td><td> $fname</td><td></td></tr>".

"<tr><td></td><td>Email</td><td></td><td>  $email</td><td></td></tr>".

"<tr><td></td><td>Company Name</td><td></td><td> $companyname</td><td></td></tr>".

"<tr><td></td><td>Address</td><td></td><td>$address</td><td></td></tr>".

"<tr><td></td><td>State</td><td></td><td>  $state</td><td></td></tr>".

"<tr><td></td><td>Country</td><td></td><td>  $country</td><td></td></tr>".

"<tr><td></td><td>Website</td><td></td><td> $website</td><td></td></tr>".

"<tr><td></td><td>Description Of Payment</td><td></td><td> $pay_desc</td><td></td></tr>".

"<tr><td></td><td>Amount  in USD</td><td></td><td><input type='text' name='amnt' id='amnt'  /> </td><td></td></tr>";

}

 

?>

<tr><td></td><td></td><td></td><td><input type='submit' name='submitted' id='submitted' value='Submit' onclick="return verify();"></td></tr></table>

</form>

This might help you:

 

<?php
        $number1 = '123';

        if (is_numeric($number1)){
            echo 'it\'s a number';
        }else{
            echo 'it\'s not a number but with (int) we can make it one <br />';
            echo (int)$number1;
        }
?>

 

Just change $number1 one in a string and see what happends if you run it. I added (int) which forces the string to be numeric. check out http://nl.php.net/manual/en/function.is-numeric.php

Here this is maybe even more usefull i added comments.

 

<?php
        //defaults
        $message = 'fill in a number'; //default message
        $amount = ''; //default value
        //
        // what to do if pressed submit and amount is not empty
        if (isset($_POST['submit'])&&!empty($_POST['amount'])){
            $amount = $_POST['amount']; // assigns submitted value

            if (is_numeric($amount)){
                $message = 'valid number';
            }else{
                $message = 'invalid value please enter valid amount';
            }
        }
        
        ?>

        <form action="" method="post">
            <ul>
                <li><input type="text" name="amount" value="<?php echo $amount; ?>" /><?php echo $message; ?></li>
                <li><input type="submit" name="submit" value="submit" /></li>
            </ul>
        </form>

This goes inside the body tag I used an unordered list instead of the table

 

-edit i changed the code a bit so it keeps the submitted amount in the field

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.