perky416 Posted March 9, 2011 Share Posted March 9, 2011 Hi Guys, Firstly im no good with regex, i tried to get my head round it however i found it really confusing. I had to get help with this code last night from other forum members. Im using the following code to validate my form fields: if(!preg_match('/^\s*\d*\.?\d{1,2}\s*$/', $price1)) { $error[] = "Your price is invalid!"; } I have 16 textboxes that i need to validate using the same preg_match, is it possible to validate them all using the above code without having to rewrite it 16 times? I tried the following but it did not work: if(!preg_match('/^\s*\d*\.?\d{1,2}\s*$/', $price1, $price2, $price3)) { $error[] = "Your price is invalid!"; } Iv also had a browse through google for using preg match with multiple form fields however i cant find info relating to my problem. Please could somebody help me out? Many thanks. Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 9, 2011 Share Posted March 9, 2011 foreach ($prices as $k => $v) { if (preg_match(yada,$v)) continue; $errors[] = 'price is invalid!'; break; } Quote Link to comment Share on other sites More sharing options...
perky416 Posted March 9, 2011 Author Share Posted March 9, 2011 Hi RussellReal, Im sorry i dont understand what you wrote, please could you explain it? Thanks Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 9, 2011 Share Posted March 9, 2011 $prices = array($price1,$price2,$price3,$price4); foreach ($prices as $k => $v) { if (preg_match(/^\s*\d*\.?\d{1,2}\s*$/,$v)) continue; $errors[] = 'price is invalid!'; break; } basically all I did was I took YOUR code, threw a loop around it, and presto Quote Link to comment Share on other sites More sharing options...
DavidAM Posted March 9, 2011 Share Posted March 9, 2011 Or, put the regex in a variable: $priceRegEx = '/^\s*\d*\.?\d{1,2}\s*$/'; if(!preg_match($priceRegEx, $price1)) { $error[] = "Your price 1 is invalid!"; } if(!preg_match($priceRegEx, $price2)) { $error[] = "Your price 2 is invalid!"; } Even better: change the form to post the prices as an array (i.e. <INPUT type="text" name="price[]"> ... Then use RussellReal's code with a slight change: foreach ($_POST['price'] as $k => $v) { if (preg_match('/^\s*\d*\.?\d{1,2}\s*$/',$v)) continue; $errors[] = 'price is invalid!'; break; } Quote Link to comment Share on other sites More sharing options...
sasa Posted March 10, 2011 Share Posted March 10, 2011 or <?php $priceRegEx = '/^\s*\d*\.?\d{1,2}\s*$/'; for($i=1;$i<=16;$i++) if(!preg_match($priceRegEx, ${'price'.$i})) $errors[]=$i; if(count($errors)>0) echo 'error in price(s) ', implode (', ', $errors); ?> Quote Link to comment Share on other sites More sharing options...
perky416 Posted March 10, 2011 Author Share Posted March 10, 2011 Hi guys thanks for lending me a hand. I decided to go with the following as visually it looks the easiest for me to understand $prices = array($price1,$price2,$price3,$price4); foreach ($prices as $k => $v) { if (preg_match('/^\s*\d*\.?\d{1,2}\s*$/,$v')) continue; $error[] = 'price is invalid!'; break; } However when i press submit on the page the following error comes up: Warning: preg_match() expects at least 2 parameters, 1 given in /home/user/public_html/websiteco.uk/rates.php on line 73 Do you have any ideas as to what is wrong? Thanks again!!! Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 10, 2011 Share Posted March 10, 2011 if (preg_match('/^\s*\d*\.?\d{1,2}\s*$/,$v')) continue; change that to if (preg_match('/^\s*\d*\.?\d{1,2}\s*$/',$v)) continue; Quote Link to comment Share on other sites More sharing options...
perky416 Posted March 10, 2011 Author Share Posted March 10, 2011 if (preg_match('/^\s*\d*\.?\d{1,2}\s*$/,$v')) continue; change that to if (preg_match('/^\s*\d*\.?\d{1,2}\s*$/',$v)) continue; Thank you it works perfectly. Quote Link to comment Share on other sites More sharing options...
perky416 Posted March 10, 2011 Author Share Posted March 10, 2011 Hi guys, Sorry i have another quick question regarding the preg match, what would i have to add to it to allow the text box to be empty? Thanks Quote Link to comment Share on other sites More sharing options...
RussellReal Posted March 10, 2011 Share Posted March 10, 2011 if (preg_match('/^(?:\s*\d*\.?\d{1,2})?\s*$/',$v)) continue; that should work >.> Quote Link to comment Share on other sites More sharing options...
perky416 Posted March 10, 2011 Author Share Posted March 10, 2011 Thank You!!!! At the weekend ill try and get my head round regex. Quote Link to comment 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.