Duminda Chamara

Saturday, August 27, 2011

How to check domain availability in PHP


How to check domain availability in PHP?


Following example shows you how to check the availability of a domain name using PHP.



Domain Name System (DNS) is a naming system for resources connected to the Internet or any other personal network. It contains details of registered domain names in the world up to date. It is a large database of domain names and the IP address mapped to that domain. A Domain Name Registrar is an organization or commercial entity that manage the reservation of Internet domain names. Domain Name Registar provides the registration service for end-users.

Before you register a domain name, you may need to check the availability of the domain name. This example is based on the www.whoisxmlapi.com Domain Availability API. First you have to create a free account with whoisxmlapi site. They will provide an account with free 500 lookups for checking the availability of domain names. Also they provide hosted whois web service plans. By using this free account you can send up to 500 lookup quires. Also they provide a way to check your remaining lookups and e-mail alert at your threshold limit. They provide an easy way to check the domain availability and whois lookup for a given domain. You can configure this to your web application using following example code.

You can get the output as JSON or XML. To do that you have to set outputFormat to JSON or XML. Note that you will be need to connect to the internet to check this example.


<?php
/*
©-Copyright by www.latestcode.net
*/


class CheckDomainNameAvailability{
const WHOIS_SERVICE_URL = "http://www.whoisxmlapi.com/whoisserver/WhoisService?";
const ACCOUNT_SERVICE_URL = "http://www.whoisxmlapi.com/accountServices.php?";

private $userName;
private $password;
private $domainName;
private $outputFormat;

private $isDomainAvailable=FALSE;
private $accountBalance;

public function __construct() {
$this->userName='username';
$this->password='password';
$this->outputFormat = 'JSON';//You can set XML also
}

/**
* Check domain name availability
* @param string $domainName
* @return boolean
*/


public function getDomainNameAvailability($domain){
$this->domainName = str_replace('www.', '', strtolower($domain));
$domain_details = @file_get_contents(self::WHOIS_SERVICE_URL."cmd=GET_DN_AVAILABILITY&domainName=".$this->domainName."&username=".$this->userName."&password=".$this->password."&outputFormat=".$this->outputFormat);
$result = json_decode($domain_details);
$this->isDomainAvailable = ($result->DomainInfo->domainAvailability==='AVAILABLE')?TRUE:FALSE;
return $this->isDomainAvailable;
}

/**
* Check available lookups
* @return bool
*/


public function getAvailableLookups(){
$accountDetails = @file_get_contents(self::ACCOUNT_SERVICE_URL."servicetype=accountbalance&username=".$this->userName."&password=".$this->password);
$xml='<xml>'.$accountDetails.'</xml>';

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);

if($xmlDoc->hasChildNodes()){
$balance = $xmlDoc->getElementsByTagName('balance');
if($balance->length>0){
$this->accountBalance = $balance->item(0)->nodeValue;
}else{
$this->accountBalance = 0;
}
unset($xmlDoc);
}else{
exit('An unexpected error has occured');
}
return $this->accountBalance;
}
}

Usage:


$dnc = new CheckDomainNameAvailability();

//To check domain name availability - you can enter domain name witho or without 'www'.

$domainaName = 'www.test.com';
$available = $dnc->getDomainNameAvailability($domainaName);
echo $domainaName." is ".(($available==FALSE)?" not ":" ")."available";

//To check account balance

$balance = $dnc->getAvailableLookups();
if($balance>0){
echo "You have ".$balance." remaining lookups";
}else{
echo "Your account balance is zero";
}

Sunday, July 10, 2011

Add or remove list box items dynamically in javascript

How to add or remove list box items dynamically using JavaScript


Following example shows you how to add or remove list items in HTML option element in JavaScript

In some occasions you may want to add one or more options to HTML select box/list box and some times remove items from a HTML dropdown/list box. You can use a simple JavaScript code to add/remove options from a HTML select element. following examples describe how to dynamically change items in a dropdown box.

1. How to Add elements/options to a list box dynamically using JavaScript?

Example : Add options/items to a list box/dropdown box/list menu in javaScript You can add any item to the dropdown box but you can't duplicate list items.

Year
Option Value
Option Display Text
 

Example code for add new option to HTML select element

<HTML xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=iso-8859-1" />
<title>Add Option Items </title>
<script type="text/javaScript">
function addNewListItem(){
var htmlSelect=document.getElementById('selectYear');
var optionValue=document.getElementById('txtYearValue');
var optionDisplaytext=document.getElementById('txtYearDisplayValue');

if(optionValue.value==''||isNaN(optionValue.value)){
alert('please enter option value');
optionValue.focus();
return false;
}
if(optionDisplaytext.value==''||isNaN(optionDisplaytext.value)){
alert('please enter option display text');
optionDisplaytext.focus();
return false;
}
if(isOptionAlreadyExist(htmlSelect,optionValue.value)){
alert('Option value already exists');
optionValue.focus();
return false;
}
if(isOptionAlreadyExist(htmlSelect,optionDisplaytext.value)){
alert('Display text already exists');
optionDisplaytext.focus();
return false;
}
var selectBoxOption = document.createElement("option");
selectBoxOption.value = optionValue.value;
selectBoxOption.text = optionDisplaytext.value;
htmlSelect.add(selectBoxOption, null);
alert("Option has been added successfully");
return true;

}
function isOptionAlreadyExist(listBox,value){
var exists=false;
for(var x=0;x<listBox.options.length;x++){
if(listBox.options[x].value==value || listBox.options[x].text==value){
exists=true;
break;
}
}
return exists;
}
</script>
</head>

<body>
<table border="0" align="left">
<tr>
<td align="right">Year</td>
<td align="left"><select name="selectYear" id="selectYear">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
</select></td>
</tr>
<tr>
<td align="right">Option Value</td>
<td align="left"><input name="txtYearValue" type="text" id="txtYearValue" /></td>
</tr>
<tr>
<td align="right">Option Display Text</td>
<td align="left"><input name="txtYearDisplayValue" type="text" id="txtYearDisplayValue" /></td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td align="left"><input name="btnAddItem" type="button" id="btnAddItem" value="Add Option" onclick="javaScript:addNewListItem();" /></td>
</tr>
</table>
</body>
</HTML>

Example 2:
Animal
Option Value
Option Display Text
 

Copy and paste above code and modify it to add string values

How to check option already exists or not?

function isOptionAlreadyExist(listBox,value){
var exists=false;
for(var x=0;x<listBox.options.length;x++){
if(listBox.options[x].value==value || listBox.options[x].text==value){
exists=true;
break;
}
}
return exists;
}

  
Example 3:Add numbers to a listbox.
Even Numbers
Option Value
Option Display Text
 

How to add new option to a HTML select element?

var htmlSelect = document.createElement("selectAnimals");//HTML select box
var optionValue=document.getElementById('txtValue');
var optionDisplaytext=document.getElementById('txtDisplayValue');

var selectBoxOption = document.createElement("option");//create new option
selectBoxOption.value = optionValue.value;//set option value
selectBoxOption.text = optionDisplaytext.value;//set option display text
htmlSelect.add(selectBoxOption, null);//add created option to select box.

2. How to remove options/items from listbox dynamically using JavaScript?

Example 1 : Remove options/items from a list box/dropdown box/list menu

Example 1:
Select a year to remove
 


Example code for remove list items from a HTML select element

<HTML xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/HTML; charset=iso-8859-1" />
<title>Remove Select Options </title>
<script type="text/javaScript">
function removeListItem(){
var htmlSelect=document.getElementById('selectYear');

if(htmlSelect.options.length==0){
alert('You have removed all options');
return false;
}
var optionToRemove=htmlSelect.options.selectedIndex;
htmlSelect.remove(optionToRemove);
alert('The selected option has been removed successfully');
return true;
}
</script>
</head>

<body>
<table border="0" align="left">
<tr>
<td align="right">select a year to remove </td>
<td align="left"><select name="selectYear" id="selectYear">
<option value="2000" selected="selected">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
</select></td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td align="left"><input name="btnRemoveItem" type="button" id="btnRemoveItem" value="Remove Option" onClick="javaScript:removeListItem();" /></td>
</tr>
</table>
</body>
</HTML>

Example 2 :Select a country and click on Remove Country button to delete them from the list menu.
Select a country to remove
 


How to remove list item from list box?

var htmlSelect=document.getElementById('selectYear');//HTML select box
var optionToRemove=htmlSelect.options.selectedIndex;//get the selected index
htmlSelect.remove(optionToRemove);//remove option from list box

Tuesday, March 15, 2011

How to validate email address in php

How to validate an email address in php?


Following example shows you how to validate an email address using PHP.


When creating online registration forms, you may need to validate user inputs. It is a good practice validate user inputs before submit the form. You can use JavaScript to validate user inputs as client-side validation. But in some cases users may have disabled execution of JavaScripts on there browsers. In such cases it is very important to use server side validate mechanism to validate user inputs, because user can't by-pass server-side validation. You can validate an email address in two ways using PHP.

  1. Validate email using Regular Expression
  2. Validate email address using filter_var() built-in php function
In HTML version 5, there is an input type called 'email'. This email input field can validate email address before user submit the form. This is browser-built-in email validation mechanism. But Old browsers which does not support rendering HTML-5 cannot understand this email input field. So please be careful using this email input in your web forms. Following example shows you how to validate an email address using PHP as server-side validation.

1. Validate an email address using regular expression with php.

PHP Code

function validateEmailAddressUsingRegularExpression($emailAddress){ $regularExpression='/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/'; if (preg_match($regularExpression,$emailAddress)) { return true; }else{ return false; } }

$emailAddress='someone@gmail.com';

if(validateEmailAddressUsingRegularExpression($emailAddress)==false){ echo 'Email address in invalid'; }else{ echo 'Email address is valid'; }

2. Validate an email address using filter_var() function in php.

PHP Code

function validateEmailAddressUsingFiltervar($email_address){ if(!filter_var($email_address, FILTER_VALIDATE_EMAIL)){ return true; }else{ return false; } }

$emailAddress='someone@gmail.com';

if(validateEmailAddressUsingFiltervar($emailAddress)==false){ echo 'Email address in invalid'; }else{ echo 'Email address is valid'; }

©-Copyright By Duminda Chamara JavaScript Validation