Automatic scroll down div content

function scrollToBottomOfdiv(divID) {
mydiv = document.getElementById(divID);
mydiv.scrollTop = mydiv.scrollHeight;
}

google search code in php

$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton"

// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, "http://www.yousite.com");
$body = curl_exec($ch);
curl_close($ch);

// following function json_decode is ready avaialable  in php 5 >
$json = json_decode($body);

display errors in php

For some good reasons some server hide php errors , still you want to display it for debugging purpose then use following simple two lines of code in a page where you want to display error

error_reporting(E_ALL);
ini_set(“display_errors”,”1″);

Check all Checkboxes

<script type=”text/javascript”>

function checkAll(fieldName, val)
{
var chkarr = document.getElementsByName(fieldName);
for(r=0;r<chkarr.length;r++)
{
if (val == true)
chkarr[r].checked = true;
else
chkarr[r].checked = false;
}
}

</script>

Check All
Mango
Banana
Apple

Date validation in javacript

validate date format e.g. 12-2-2004,12-11-08
function validateDate()
{
obj=//name of your object
msg=//your mesage comes here

var validNum = /^([0-9]{1,2})-([0-9]{1,2})-([0-9]{2,4})$/;
if (validNum.test(obj.value) == false)
{
alert(msg);
obj.focus();
//obj.select();
return false;
}
return true;
}

Validation of Url

function validateUrl()
{

obj=document.getElementById(‘txtUrl’);

msg=”Enter valid url”;
var urlStr = /^\http\:\/\/[a-zA-Z]{3,}\.[a-zA-Z0-9]{2,}
(\.[a-zA-Z]{2,3}|\.[a-zA-Z]{2,3}\.[a-zA-Z]{2})$/;
if (urlStr.test(obj.value) == false)
{
alert(msg);
obj.focus();
obj.select();
return false;
}
return true;
}

Email validation using javascript

function validateEmail()

{

//name of email input field

obj=document.getElementById(‘txtEmail’);

msg=”Please enter valid email address”;
var emailStr = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z]
[a-zA-Z\.]*[a-zA-Z]$/;
if (emailStr.test(obj.value) == false)

{
alert(msg);
obj.focus();
obj.select();
return false;
}
return true;
}

Php Server side security using preg_match.

To avoid the cross side scripting or make site safe from hacker attacks one need to perform data validation before allowed data insertion into database . We can achieve this using php preg_match which check data format to see whether it is in valid state or not. The following code snippet is standard validation example

function isHackerSafeUsername($Subject)
{
if( preg_match(“/^[a-zA-Z][\w\._]*[a-zA-Z0-9]$/”,$Subject)) return true;
else return false;
}
function isHackerSafePassword($Subject)
{
if( preg_match(“/[^a-zA-Z0-9@._'-]/”,$Subject)) return false;
else return true;
}

function isHackerSafeName($Subject)
{
if( preg_match(“/^[a-zA-Z]{1,}$/”,$Subject)) return true;
else return false;
}
function isHackerSafeEmail($Subject)
{
if( preg_match(“/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*
[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]/”,$Subject)) return true;
else return false;
}
function isHackerSafeLan($Subject)
{
if( preg_match(“/^[a-zA-Z]{1}$/”,$Subject)) return true;
else return false;
}
function isHackerSafeNumber($Subject)
{
if( preg_match(“/^[0-9]{1,15}$/”,$Subject)) return true;
else return false;
}

function isHackerSafeAddress($Subject)
{
if( preg_match(“/^[a-zA-Z0-9\s,]{1,}$/”,$Subject)) return true;
else return false;
}

function isHackerSafeTitle($Subject)
{
if( preg_match(“/^[a-zA-Z0-9\s,]{1,}$/”,$Subject)) return true;
else return false;
}

function isHackerSafeCityState($Subject)
{
if( preg_match(“/^[a-zA-Z\s]{1,}$/”,$Subject)) return true;
else return false;
}
function isHackerSafeAnswer($Subject)
{
if( preg_match(“/^[a-zA-Z0-9\s]{1,}$/”,$Subject)) return true;
else return false;
}
function isHackerSafeQuestion($Subject)
{
if( preg_match(“/^[a-zA-Z]{1,}$/”,$Subject)) return true;
else return false;
}
?>

phone no validation using js

function validatePhone(obj,msg)
{
var validNum =/^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})[\s]?[x]?(\d*)$/;
if (validNum.test(obj.value) == false)
{
alert(msg);
obj.focus();
obj.select();
return false;
}
return true;
}

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!