Checking input date of a form ( Validation )

Once we receive any external data from the user ( via web form ) or from any other sources ( than the script itself ) , we must validate or check it before using them. Here are some sample codes can be used.

Only Integer is allowed

$number=5236;
if(is_int($number)){
echo "Validation passed ";
}else{
echo "Validation failed ";
}

Only Integer & Float is allowed ( received from Web Form)

$userid=$_POST['userid']; // If form method is POST
if(is_numeric($userid)){
echo "Validation passed ";
}else{
echo "Validation failed ";
}

Only alphabetic characters are allowed

$userid=$_POST['userid']; // If form method is POST
if(ctype_alpha($userid)){
echo "Validation passed ";
}else{
echo "Validation failed ";
}

Only number and characters are allowed

$userid=$_POST['userid']; // If form method is POST
if(ctype_alnum($userid)){
echo "Validation passed ";
}else{
echo "Validation failed ";
}

Only number, character and space are allowed ( without regular expression)

$state='abc de';
if ((strlen($state)) > 0 and !ctype_alpha(str_replace(' ', '', $state)) === true) {
echo "Validation failed ";
}else{
echo "Validation passed ";
}

String of particular length only allowed.

$userid='abc1df';
if(strlen($userid) == 6){
echo "Validation passed ";
}else{
echo "Validation failed ";
}

String of minimum and maximum length is only allowed.

$userid=trim('abcde');
if(strlen($userid) > 3 and strlen($userid)< 8){
echo "Validation passed ";
}else{
echo "Validation failed ";
}
While using length calculations it is better to use trim() function to remove blank space at left and right side of the variable.

Validation of Date

signed userid is already present in table or not. Checked by using PDO and SQL

$no = $dbo->query("select count(userid) from mem_signup where userid='$userid'")->fetchColumn(); 

if($no >0 ){
$msg=$msg."User Name already exists. Choose a different User Name";
}

Email address validation

We missed anything here ? Let us know ( post it here )
Visitors Rating
Your Rating





Post Comment This is for short comments only. Use the forum for more discussions.




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer