Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,660 members, 7,801,914 topics. Date: Friday, 19 April 2024 at 05:35 AM

Preg_match Problem In Php - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Preg_match Problem In Php (649 Views)

Solution To Tokenmismatchexception Problem In Laravel 5.1 / Simple Array Problem In Php / Running Python Codes In Php (2) (3) (4)

(1) (Reply) (Go Down)

Preg_match Problem In Php by ayodeleolu2014: 8:05pm On Jul 25, 2014
I'm currently working on a small web application called expenses whereby can save your expenses to a mysql db via php array.

Here is how it works.

In the first page, you'll have to enter the number of items that you want to save their expenses to the mysql db.

So, let's assume that you enter 3 as the number of items, you'll be redirected to another page whereby you'll be provided with textboxes where you'll enter the name of the items and their corresponding amounts and then click on save button to save it to the database.

So, how can i restrict the amount textboxes to accept only numeric characters and a dot in such a way that numeric character must start, then followed by a dot (which is optional) and it must also end with a numeric character.

e.g 100.8 , 250.4 , 250, 300.8 , 200.

This is my code


foreach ($amount as $digit)
{
if((!ctype_digit($digit)) || ($digit<1) || (!preg_match("/[\.]$/",$digit)))
{
echo " Only positive, numeric, non-zero digits and a dot are allowed for amount of item";
exit();
}
}

I'll be glad if you help me correct this because it's been up to 2 days now that I've been battling with this.

THANKS AND GOD BLESS!
Re: Preg_match Problem In Php by ayodeleolu2014: 1:48am On Jul 26, 2014
Programmers in nairaland, please help.. dnt just view this thread!
Re: Preg_match Problem In Php by phpier: 5:35am On Jul 26, 2014
.......... Try this , if(! preg_match( "/[\ .]+$/s" ,$digit)) ;
Re: Preg_match Problem In Php by phpier: 5:36am On Jul 26, 2014
..............
Re: Preg_match Problem In Php by Olyboy16(m): 8:54pm On Jul 26, 2014
Why preg_match ? If i'm to b precise you want the input to be a float number right? Then first check if d user has entered a valid positive numeric value with somfin lyk (is_numeric() !< 0 ) i hope u undastand d logic dia, and then type cast to float like this.. (float)$input;
shikena!

(1) (Reply)

Malware Research And Awareness ? / Power Challenge / Javafx8

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 9
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.