bindiya Posted December 27, 2010 Share Posted December 27, 2010 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/ Share on other sites More sharing options...
QuickOldCar Posted December 27, 2010 Share Posted December 27, 2010 http://php.net/manual/en/function.is-numeric.php Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151713 Share on other sites More sharing options...
bindiya Posted December 27, 2010 Author Share Posted December 27, 2010 but my code is itself inside an echo at that time how will i check Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151719 Share on other sites More sharing options...
revraz Posted December 27, 2010 Share Posted December 27, 2010 You can put PHP inside of a echo string. Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151722 Share on other sites More sharing options...
xProteuSx Posted December 27, 2010 Share Posted December 27, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151728 Share on other sites More sharing options...
bindiya Posted December 27, 2010 Author Share Posted December 27, 2010 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151733 Share on other sites More sharing options...
litebearer Posted December 27, 2010 Share Posted December 27, 2010 It appears the op is looking to validate user input at the form level, ie client-side. That would require javascript. Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151735 Share on other sites More sharing options...
bindiya Posted December 27, 2010 Author Share Posted December 27, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151738 Share on other sites More sharing options...
fortnox007 Posted December 27, 2010 Share Posted December 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151743 Share on other sites More sharing options...
fortnox007 Posted December 27, 2010 Share Posted December 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/222711-how-will-i-check-whether-the-characters-i-am-entering-is-numeric/#findComment-1151744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.