hello friends
i am using preg_match instead of earg .....see the below code......
if(preg_match('/[^0-9]/', $bsr))
{
die ("Please enter only numeric value.");
}
but this code is not working . can anyone suggest me what mistake i am doing here..................
thank you
help: ereg() is deprecated so what to use
Moderator: General Moderators
-
om.bitsian
- Forum Commoner
- Posts: 36
- Joined: Wed Sep 09, 2009 4:13 am
Re: help: ereg() is deprecated so what to use
well i did notice you are using ^ in your match, which taken from the php cheat sheet:
[^abc] - Not in range (not a, b or c)
[^abc] - Not in range (not a, b or c)
Code: Select all
$bsr = "1ab2cd3ef";
if(preg_match('/[0-9]/', $bsr,$out)){
print "numbers only";
print_r($out); // this is the first matched item
exit;
}else{
print "no numbers found";
}-
om.bitsian
- Forum Commoner
- Posts: 36
- Joined: Wed Sep 09, 2009 4:13 am
Re: help: ereg() is deprecated so what to use
the smme function is not working here
<label>2. BSRN</label>
<input type="text" name="bsr" value="0" onclick='highlight(this);' >
$bsr=$_POST['bsr'];
if(preg_match('/[^0-9]/', $bsr))
{
die ("Please enter only numeric value.");
}
but the same code is working here
<label>15. Allowance without PF</label>
<input type="text" name="alwopf" value="<?php if (isset($_POST['submit'])) echo $_POST['alwopf'];?>">
$alwopf=($_POST['alwopf']);
if(preg_match('/[^0-9]/', $alwopf))
{
die ("Allowance without PF should be a numeric value.");
}
i dont know why??
<label>2. BSRN</label>
<input type="text" name="bsr" value="0" onclick='highlight(this);' >
$bsr=$_POST['bsr'];
if(preg_match('/[^0-9]/', $bsr))
{
die ("Please enter only numeric value.");
}
but the same code is working here
<label>15. Allowance without PF</label>
<input type="text" name="alwopf" value="<?php if (isset($_POST['submit'])) echo $_POST['alwopf'];?>">
$alwopf=($_POST['alwopf']);
if(preg_match('/[^0-9]/', $alwopf))
{
die ("Allowance without PF should be a numeric value.");
}
i dont know why??
- turbolemon
- Forum Commoner
- Posts: 70
- Joined: Tue Jul 14, 2009 6:45 am
- Location: Preston, UK
Re: help: ereg() is deprecated so what to use
This is the desired behaviour.. validate a numeric field by matching non-numeric characters. preg_match will return 0 for a valid field, and 1 for an invalid field.Weiry wrote:well i did notice you are using ^ in your match, which taken from the php cheat sheet:
[^abc] - Not in range (not a, b or c)
How is it not working..? If only the first error is showing (assuming both inputs submitted and validated at the same time), then it's because you are killing the script at the first error. You would be much better off just echo'ing the error beneath the input; the script can then output the complete form to try again.
Re: help: ereg() is deprecated so what to use
Crap, i cant believe i actually thought the opposite.. maybe i need a break from this damn PHP assignment im doingturbolemon wrote: This is the desired behaviour.. validate a numeric field by matching non-numeric characters.
Sorry about the misinformation.
Re: help: ereg() is deprecated so what to use
I'd personally use: as \d is a shortcut meaning the digits 0-9
Code: Select all
/^\d/Real programmers don't comment their code. If it was hard to write, it should be hard to understand.