Validating using ereg function??

i need some help about validating icno(identity card no eg: 820411-03-5325) and dob(date of birth eg: 1982-04-11). how am i suppose to use ereg function which can validate a dash?? following is the code that i use…

[code]<?php
include(“conn.inc”);

$label_array = array("fname" => "First Name",
					 "lname" => "Last Name",
					 "icno" => "IC Number",
					 "uname" => "Username",
					 "pass" => "Password",
					 "dob" => "Date of Birth",
					 "addr" => "Address",
					 "pcode" => "Postcode",
					 "htel" => "Home Telephone Number",
					 "otel" => "Office Telephone Number",
					 "mtel" => "Mobile Telephone Number",
					 "alg" => "Allergic",
					 "occpy" => "Occupation");
					 
foreach($_POST as $key => $value)
{
	if($value == "")
	{
		if($key != "addr" and $key != "pcode" and $key != "htel" and $key != "otel" and $key != "mtel" and $key != "alg" and $key != "occpy")
		{
			$blank_array[$key] = "blank";
		}
	}
	elseif($key == "fname" or $key == "lname")
	{
		if(!ereg("^[A-Za-z' -]{1,50}$",$_POST[$key]))
		{
			$bad_format[$key] = "bad";
		}
	}
	elseif($key == "icno")
	{
		if(!ereg('^([0-9]{6})(-[0-9]{2})?(-[0-9]{4})?$',$key))
		{
			$bad_format[$key] = "bad";
		}
	}
	elseif($key == "dob")
	{
		if(!ereg('^([0-9]{4})[-|/]?([0-9]{2})[-|/]?([0-9]{2})$',$key))
		{
			$bad_format[$key] = "bad";
		}
	}
}

if(@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0)
{
	if(@sizeof($blank_array) > 0)
	{
		echo "<b>You didn't fill one or more of the required field(s).</b><br>";
		echo "<b>Please enter value for : </b>";
		foreach($blank_array as $key => $value)
		{
			echo "{ $label_array[$key] } ";
		}
	}
	
	if(@sizeof($bad_format) > 0)
	{
		echo "<b>One or more fields appears to be incorrect</b><br>";
		echo "<b>Please correct the following fields : </b>";
		foreach($bad_format as $key => $value)
		{
			echo "( {$label_array[$key] } ";
		}
	}
	include("add_pat.php");
	exit();
}

	 	
$query="SELECT * FROM patient";

#$result=mysql_query($query,$conn);

if(mysql_query($query,$conn)){
	$sql="INSERT INTO patient VALUES('', '$_POST[fname]', '$_POST[lname]', '$_POST[uname]', '$_POST[pass]', '$_POST[icno]', '$_POST[dob]', '$_POST[addr]', '$_POST[pcode]', '$_POST[htel]', '$_POST[otel]', '$_POST[mtel]', '$_POST[gend]', '$_POST[alg]' , now(), '$_POST[occpy]','' )";
	$result2 = mysql_query($sql,$conn) or die (mysql_error());
	echo "<p align="center"><b><font size="1" color="000080" face="Arial">Patient has been added into database!!</font></b></p>";
	include("add_pat.php");
}
else{
	echo "Error adding patient into database!";
	include("add_pat.php");
}	

?>[/code]

What is the problem? What is or isn’t your code doing? What errors are you getting?

opss…sorry… :oops:
the error is here

elseif($key == "icno") { if(!ereg('^([0-9]{6})(-[0-9]{2})?(-[0-9]{4})?$',$key)) { $bad_format[$key] = "bad"; } } elseif($key == "dob") { if(!ereg('^([0-9]{4})[-|/]?([0-9]{2})[-|/]?([0-9]{2})$',$key)) { $bad_format[$key] = "bad"; } }

i dont know why…coz it keeps getting “bad”… is my ereg function ok??

I would have to say sorry, but I am not good with ereg() at all…Sorry. But is there anyway you could use… str_pos(), substr() to get what you need?

http://www.php.net/str_pos
http://www.php.net/substr

Well I doubt you want it to be matching the key…you probably would want the $value in there instead:

elseif($key == "icno") { if(!ereg('^([0-9]{6})(-[0-9]{2})?(-[0-9]{4})?$',$value)) { $bad_format[$key] = "bad"; } } elseif($key == "dob") { if(!ereg('^([0-9]{4})[-|/]?([0-9]{2})[-|/]?([0-9]{2})$',$value)) { $bad_format[$key] = "bad"; } }

As you had it there, it would check “icno” or “dob” for a match, and of course it’s going to come out as bad, lol. Simple mistake to make though!

One more thing to mention - about the icno, a person could match that regex when only putting in the first six numbers. I don’t know whether you want it like that, but if not, you want something more like this:

^([0-9]{6})(-[0-9]{2})(-[0-9]{4})$

…without the ?'s in there. It’s up to you though, I’m just guessing.

Sponsor our Newsletter | Privacy Policy | Terms of Service