Archive for the ‘JAVASCRIPT’ Category

Mastered the accordion with MooTools

Wednesday, June 3rd, 2009

Call accordion those dynamic elements of our Web pages that show an item to move the mouse (or click on it) to conceal the other components. This technique very convenient for displaying large amounts of information in a small space usually give a headache to match all the information.

byslidermenu

With BySlideMenu never have problems. This is a MooTools plugin that allows us to make a wide variety of accordions, with very few lines of code.

HTML:

The HTML code we use to generate our accordion will be based on a list of elements:

<ul id="pinclickmenu">
    <li><img src="creditcards.jpg" /></li>
    <li><img src="games.jpg" /></li>
    <li><img src="computer.jpg" /></li>
    <li><img src="eiffeltower.jpg" /></li>
    <li><img src="electronic.jpg" /></li>
</ul>

Each <li/> will be one of the elements that make up the accordion.

JavaScript:

?View Code JAVASCRIPT
var pinclickmenu = new BySlideMenu('pinclickmenu', {pinMode: 'click'});

As we see fewer lines is impossible :D

Demos and Downloads:

You can see more demos and download the plugin from the project page.

Happy Programming!!! ;-)

Validate: Username, Email Id, Phone No, Zip Code & URL In 5 Languages(JS, C#.Net, VB.Net, JAVA & PHP)

Monday, March 23rd, 2009

Validate of Username, Email Id, Phone No, Zip/Postal Code & URL In Javascript, C#.Net, VB.Net, JAVA & PHP !

1. Validate username: Validate alpha numeric values
2. Validate email addresses : example@domain.com
3. Validate Phone numbers: Validate US phone number eg: 123-456-7890
4. Validate Zip/Postal Codes: Validate US Zip/Postal Code eg: 12345-6789
5. Validate Domain Name eg: http://live.com

Validate username: Validate alpha numeric values

?View Code JAVASCRIPT
function is_valid_username(str_username)
{
 var filter = /^([a-zA-Z\s0-9]*)$/;
 if (!filter.test(str_username))
 {
 alert(’Wrong username format.’);
 }
 else
 {
 alert('Username format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_username(string str_username)
 {
 Regex matchRegex = new Regex(@"^[a-zA-Z0-9_]{3,16}$");
 MatchCollection matches  = matchRegex.Matches(str_username);
 if(matches.Count==0)
 {
 Response.Write("Wrong username format."); // In Webserver
 //MessageBox.Show("Wrong username format."); //In Windows Form
 }
 else
 {
 Response.Write("Username format is ok."); // In Webserver
 //MessageBox.Show("Username format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_username(ByVal str_username As String)
    Dim matchRegex As New Regex("^[a-zA-Z0-9_]{3,16}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_username)
    If matches.Count = 0 Then
        Response.Write("Wrong username format.") ' In Webserver
        'MessageBox.Show("Wrong username format."); 'In Windows Form
    Else
        Response.Write("Username format is ok.")    ' In Webserver
        'MessageBox.Show("Username format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
 
public class is_valid_username
{
 public static void main(String str_username)
 {
 Pattern p=Pattern.compile("^[A-Za-z0-9]+$");
 Matcher m=p.matcher(str_username);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Username format is ok.");
 }
 else
 {
 System.out.println("Wrong username format.");
 }
 }
}
Function is_valid_username($str_username)
{
 if (preg_match('/^[a-z\d_]{5,20}$/i',$str_username))
 {
     echo "Username format is ok.";
 }
 else
 {
     echo "Wrong username format.";
 }
}

Validate email addresses : example@domain.com

?View Code JAVASCRIPT
function is_valid_email(str_email_id)
{
 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 if (!filter.test(str_email_id))
 {
 alert(’Wrong email address format.’);
 }
 else
 {
 alert('Email address format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 private void is_valid_email(string email_id)
 {
 Regex matchRegex = new Regex(@"[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}");
 MatchCollection matches  = matchRegex.Matches(email_id);
 if(matches.Count==0)
 {
 Response.Write("Wrong email address format."); // In Webserver
 //MessageBox.Show("Wrong email address format."); //In Windows Form
 }
 else
 {
 Response.Write("Email address format is ok."); // In Webserver
 //MessageBox.Show("Email address format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_email(ByVal email_id As String)
    Dim matchRegex As New Regex("[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}")
    Dim matches As MatchCollection = matchRegex.Matches(email_id)
    If matches.Count = 0 Then
        Response.Write("Wrong email address format.") ' In Webserver
        'MessageBox.Show("Wrong email address format."); 'In Windows Form
    Else
        Response.Write("Email address format is ok.")    ' In Webserver
        'MessageBox.Show("Email address format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_email
{
 public static void main(String str_email_id)
 {
 Pattern p=Pattern.compile("[a-zA-Z]*[0-9]*@[a-zA-Z]*.[a-zA-Z]*");
 Matcher m=p.matcher(str_email_id);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Email address format is ok.");
 }
 else
 {
 System.out.println("Wrong email address format.");
 }
 }
}
Function is_valid_email($email_id)
{
 if (preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email_id))
 {
     echo "Email address format is ok.";
 }
 else
 {
     echo "Wrong email address format.";
 }
}

Validate Phone numbers: Validate US phone number eg: 123-456-7890

?View Code JAVASCRIPT
function is_valid_phone_no(str_phone_no)
{
 var filter =  /^((\+?1-)?\d\d\d-)?\d\d\d-\d\d\d\d$/;
 if (!filter.test(str_phone_no))
 {
 alert(’Wrong phone number format.’);
 }
 else
 {
 alert('Phone number format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_phone_no(string str_phone_no)
 {
 Regex matchRegex = new Regex(@"\d{3}-\d{3}-\d{4}$");
 MatchCollection matches  = matchRegex.Matches(str_phone_no);
 if(matches.Count==0)
 {
 Response.Write("Wrong phone number format."); // In Webserver
 //MessageBox.Show("Wrong phone number format."); //In Windows Form
 }
 else
 {
 Response.Write("Phone number format is ok."); // In Webserver
 //MessageBox.Show("Phone number format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
Private Sub is_valid_phone_no(ByVal str_phone_no As String)
    Dim matchRegex As New Regex("\d{3}-\d{3}-\d{4}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_phone_no)
    If matches.Count = 0 Then
        Response.Write("Wrong phone number format.") ' In Webserver
        'MessageBox.Show("Wrong phone number format."); 'In Windows Form
    Else
        Response.Write("Phone number format is ok.")    ' In Webserver
        'MessageBox.Show("Phone number format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_phone_no
{
 public static void main(String str_phone_no)
 {
 Pattern p=Pattern.compile("^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$");
 Matcher m=p.matcher(str_phone_no);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("Phone number format is ok.");
 }
 else
 {
 System.out.println("Wrong phone number format.");
 }
 }
}
Function is_valid_phone_no($str_phone_no)
{
 if (preg_match('/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x',$str_phone_no))
 {
     echo "Phone number format is ok.";
 }
 else
 {
     echo "Wrong phone number format.";
 }
}

Validate Zip/Postal Codes: Validate US Zip/Postal Code eg: 12345-6789

?View Code JAVASCRIPT
function is_valid_zip_code(str_zip_code)
{
 var filter =  /^\d\d\d\d\d-\d\d\d\d$/;
 if (!filter.test(str_zip_code))
 {
 alert(’Wrong Zip/Postal Code format.’);
 }
 else
 {
 alert('Zip/Postal Code format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 
 private void is_valid_zip_code(string str_zip_code)
 {
 Regex matchRegex = new Regex(@"\d{5}-\d{4}$");
 MatchCollection matches  = matchRegex.Matches(str_zip_code);
 if(matches.Count==0)
 {
 Response.Write("Wrong Zip/Postal Code format."); // In Webserver
 //MessageBox.Show("Wrong Zip/Postal Code format."); //In Windows Form
 }
 else
 {
 Response.Write("Zip/Postal Code format is ok."); // In Webserver
 //MessageBox.Show("Zip/Postal Code format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
Private Sub is_valid_zip_code(ByVal str_zip_code As String)
    Dim matchRegex As New Regex("\d{5}-\d{4}$")
    Dim matches As MatchCollection = matchRegex.Matches(str_zip_code)
    If matches.Count = 0 Then
        Response.Write("Wrong Zip/Postal Code format.") ' In Webserver
        'MessageBox.Show("Wrong Zip/Postal Code format."); 'In Windows Form
    Else
        Response.Write("Zip/Postal Code format is ok.")    ' In Webserver
        'MessageBox.Show("Zip/Postal Code format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_zip_code
{
 public static void main(String str_zip_code)
 {
 Pattern p=Pattern.compile("^(\\d{5})[- ]?(\\d{4})$");
 Matcher m=p.matcher(str_zip_code);
 boolean matchFound = m.matches();
if(matchFound)
 {
 System.out.println("Zip/Postal Code format is ok.");
 }
 else
 {
 System.out.println("Wrong Zip/Postal Code format.");
 }
 }
}
Function is_valid_zip_code($str_zip_code)
{
 if (preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$str_zip_code))
{
     echo "Zip/Postal Code format is ok.";
 }
 else
 {
     echo "Wrong Zip/Postal Code format.";
 }
}

Validate Domain Name eg: http://live.com

?View Code JAVASCRIPT
function is_valid_url(str_url)
{
 var filter =   /^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s&lt;&gt;\#%"\,\{\}\\|\\\^\[\]`]+)?$/
 if (!filter.test(str_url))
 {
 alert(’Wrong URL format.’);
 }
 else
 {
 alert('URL format is ok.')
 }
 return false;
}
?View Code CSHARP
 using System.Text.RegularExpressions;
 private void is_valid_url(string str_url)
 {
Regex matchRegex = new Regex(@"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?");
 MatchCollection matches  = matchRegex.Matches(str_url);
 if(matches.Count==0)
 {
 Response.Write("Wrong URL format."); // In Webserver
 //MessageBox.Show("Wrong URL format."); //In Windows Form
 }
 else
 {
 Response.Write("URL format is ok."); // In Webserver
 //MessageBox.Show("URL format is ok."); //In Windows Form
 }
 }
Imports System.Text.RegularExpressions
 
Private Sub is_valid_url(ByVal str_url As String)
    Dim matchRegex As New Regex("(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?")
    Dim matches As MatchCollection = matchRegex.Matches(str_url)
    If matches.Count = 0 Then
        Response.Write("Wrong URL format.") ' In Webserver
        'MessageBox.Show("Wrong URL format."); 'In Windows Form
    Else
        Response.Write("URL format is ok.")    ' In Webserver
        'MessageBox.Show("URL format is ok."); 'In Windows Form
    End If
End Sub
import java.util.regex.*;
public class is_valid_url
{
 public static void main(String str_url)
 {
 Pattern p=Pattern.compile("(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?");
 Matcher m=p.matcher(str_url);
 boolean matchFound = m.matches();
 if(matchFound)
 {
 System.out.println("URL format is ok.");
 }
 else
 {
 System.out.println("Wrong URL format.");
 }
 }
}
Function is_valid_url($str_url)
{
 if (preg_match('/^([0-9]{5})(-[0-9]{4})?$/i',$str_url))
 {
     echo "URL format is ok.";
 }
 else
 {
     echo "Wrong URL format.";
 }
}

Multi-Language In Any Web Page With Google API

Monday, March 16th, 2009

Multi-language In Any Web Page (HTML, PHP, ASP.NET etc) With Google API

live_preview Demo      downloadDownload Code

Mostly Web Programmers Multilanguage in ASP.NET is by using Localization & Resource rules. But this rule cant use with HTML, PHP etc. Now I’m changing to new rules with Google AJAX Language API. It is very useful for programmers. Click here to read about Localization & Resource

Advantage & Disadvantage

Advantage:
1: Reduce programming time
2. Reduce file size, In Localization & Resource rules, we need resource files by each language
3. No need to find meaning of unknown language for creating site
4. Only one language to store in XML or..5. No need C# & VB.NET.

Disadvantage:
1. Need Internet connection for connecting to Google AJAX Language API’s Script

Click here to read about Google Language AJAX API.
I have a sample program as below. I Hope you will understood.

 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
 
<script type="text/javascript">
 
google.load("language", "1");
 
 
function fnTrans(strTransText,strTransField, strTransFLang, strTransTLang)
{
 // Translation
 google.language.translate(strTransText, strTransFLang, strTransTLang, function(result) {
          var strcontainer = document.getElementById(strTransField);
          strcontainer.innerHTML = result.translation ;
 
      });
}
 
function initialize(strToTxtBox, strFromTxtBox) {
 
 // Translate English to German
 
 strFLang = 'en' //English
 strTLang = 'de' //German
 
 var strText = document.getElementById(strFromTxtBox).innerHTML; //Get Lable Name
 
 fnTrans(strText,strToTxtBox, strFLang,strTLang)
 
}
 
 function fnhi()
  {
 
  initialize('ToLableName_FN', 'FrmLableName_FN' ); // First Name
 
  initialize('ToLableName_LN', 'FrmLableName_LN' ); // Last Name
 
  }
</script>
 
  </head>
  <body onload="fnhi()">
 
      <label id="FrmLableName_FN" >First Name</label>
      <br />
  <label id="ToLableName_FN"></label>
  <input type="text" name="txtFname" />
</br>
</br>
 
      <label id="FrmLableName_LN" >Last Name</label>
      <br />
  <label id="ToLableName_LN"></label>
  <input type="text" name="txtFname" />

live_preview Demo      downloadDownload Code