Page 1 of 1

help: ereg() is deprecated so what to use

Posted: Fri Sep 11, 2009 5:47 am
by om.bitsian
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

Re: help: ereg() is deprecated so what to use

Posted: Fri Sep 11, 2009 6:18 am
by Weiry
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)

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";
    }

Re: help: ereg() is deprecated so what to use

Posted: Fri Sep 11, 2009 6:53 am
by om.bitsian
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??

Re: help: ereg() is deprecated so what to use

Posted: Fri Sep 11, 2009 7:17 am
by turbolemon
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)
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.

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

Posted: Fri Sep 11, 2009 7:23 am
by Weiry
turbolemon wrote: This is the desired behaviour.. validate a numeric field by matching non-numeric characters.
Crap, i cant believe i actually thought the opposite.. maybe i need a break from this damn PHP assignment im doing :banghead:
Sorry about the misinformation.

Re: help: ereg() is deprecated so what to use

Posted: Fri Sep 11, 2009 2:16 pm
by pickle
I'd personally use:

Code: Select all

/^\d/
as \d is a shortcut meaning the digits 0-9