Subscribe For Free Updates!

We'll not spam mate! We promise.

Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Aug 14, 2014

htmlentities() and html_entity_decode() in JQuery

htmlentities() and html_entity_decode() in JQuery :

htmlentities() and html_entity_decode() in JQuery
We are all know about use of htmlentities() in PHP. This function is used to convert all character to HTML entities. both function are used to treat string as a html and HTML as a string.
  
    For Example :
  
              
            echo htmlentities("TechniqZone");
          
         ?>

      
        Output :
      
            &lt ;a href=&#39 ;techniqzone.blogspot.in'&#39 ;&gt ;TechniqZone&lt ;/a&gt ; 
 // here semicolons (;) are placed with spaces for display here.
           
        This is very usefull in many places in php files to display html tags in website. But in javascript/JQuery, pre-defined encode decode function is not available.
      
        I hereby explained how to use encode and decode of HTML tags in JQuery. Before that need to know the diifference between JQuery.html() and JQuery.text().
      
            JQuery.html() - Treat string as HTML.
            JQuery.html() - Treat HTML as string.

          
        For Example :
      
            $("#techZone").html("<a href=''></a>");
            $("#techZone").text("<a href=''></a>");


Okay, now we create encode/decode custom function using JQuery.
  
        function htmlentities(str)
        {
            if(str) // check str is empty or not.
                return $("<div />").text(str).html();
            else
                return 'Argument mis-matched';
        }

      
        htmlentities("TechniqZone");
      
     Here create dummy <div> tag and append given string as text, after that take that dummy div text as html content.
  
     For decode,
  
        function html_entity_decode(str)
        {
            if(str) // check str is empty or not.
                return $("<div />").html(str).text();
            else
                return 'Argument mis-matched';
        }

  
        html_entity_decode("&lt ;a href=&#39 ;techniqzone.blogspot.in&#39 ;&gt ;TechniqZone&lt ;/a&gt ;");
// here semicolons (;) are placed with spaces for display here.

Note : htmlentities and html_entity_decode function are only working with jquery plugin files.

Aug 25, 2013

Email validation using JavaScript and Jquery

Email validation using JavaScript and Jquery :

Email validation in Jquery - TechniqZone

 Here i will explain how to validate email id with JQuery. In JavaScript we can check the email id format only. but in jquery we can check whether we allow or not some of domain names also. First i will explain basic javascript method to validate email id.

Here is Javascript method :

function validateEmail(emailId)
{
var condition = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return condition.test(emailId);
}
In this javascript method only validate the given email id has alphabets and numeric. Here the variable "condition" has the email conditions. that is

([a-zA-Z0-9_.+-]) - is to allow email id username with  alphabets and numeric and also _.
@(([a-zA-Z0-9-]) - is to allow domail name with alphabets and numeric . and
([a-zA-Z0-9]{2,4}) - is to allow last part of the email id i.e., com, in, .. with alphabets and numeric and also                                   it's length should be 2 to 4.

Now i will explain how to validate email id with JQuery.

Here is JQuery Method :

$(document).ready(function() {

  $('#submitButton').click(function() {

    var emailCon = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var emailblockCon =/^([\w-\.]+@(?!gmail.com)(?!yahoo.com)([\w-]+\.)+[\w-]{2,4})?$/;

    var emailId = $("#mailId").val();
    if(emailId == '') {
      $("#mailId").val('Email id is Blank.');
      error = true
    }

    else if(!emailCon.test(emailId)) {
      $("#mailId").val('Check Your Email id.');
      error = true
    }

    else if(!emailblockCon.test(emailId)) {
      $("#mailId").val('gmail and yahoo mail ids not accept.');
      error = true
    }

    if(error == true) {
           return false;
    }

    });
}); 

In this JQuery method you can check the email format like in javascript . In jquery we validate extra one condition. here we validate the email id domain name also. because some of the website doesnt allow some free email providers domain. so we can also validate it here.


Jun 16, 2013

Javascript popup window to return value to parent window

Javascript popup window to return value to parent window:

 We can pass the values to child window to parent window by using javascript. These passing methodology working with html tags and javascript functions.

The javascript popup window is used to get the external value like select the region from map etc.

Follow the below html programs to pass the value from javascript popup window..

parent.html :

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Opener</title>
<script type='text/javascript'>
function valideopenerform(){
var popy= window.open('popup.html','popup_form','location=no,menubar=no,status=no,top=50%,left=50%,height=550,width=750')
}
</script>
</head>

<body>
<form name='form1' id='form1' >
<input type='text' id='text1' name='text1' />
<input type='button' value='go' onclick='valideopenerform()' />
</form>

</body>

</html> 

 In this first html valideopenerform() is i used. in this function i called the second html for open as a popup window. and createone text field with the id "text1". i '' return the value to this text field from popup window.

popup.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Opener</title>
<script type='text/javascript'>
function validepopupform(){
window.opener.document.form1.text1.value=document.form2.text2.value;
self.close();
}
</script>

</head>

<body>
<form id='form2' name='form2' >
<input type='text' id='text2' name='text2' />
<input type='button' value='go' onclick='validepopupform()' />
</form>

</body>


</html> 

 In this second html i used validepopupform() to return the value to parent window text field by using text field id(text1).

Now the value passed to the parent window text field.


May 19, 2013

Use JSON in PHP 4 or PHP 5.1.x

Use JSON in PHP 4 or PHP 5.1.x : 


Here i will explain how to use JSON (json_encode() , json_decode()) in versions of PHP earlier than 5.2. Add the following custom function to use (json_encode() , json_decode()) this functions in php. IF u don't have this JSON PEAR Package You can download in HERE 

1. for json_encode() :

if (!function_exists('json_encode')) {
    function json_encode($content) {
        require_once 'classes/JSON.php';
        $json = new Services_JSON;
        return $json->encode($content);
    }
}

2. for json_decode() :

if (!function_exists('json_decode')) {
    function json_decode($content, $assoc=false) {
        require_once 'classes/JSON.php';
        if ($assoc) {
            $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        }
        else {
            $json = new Services_JSON;
        }
        return $json->decode($content);
    }
}


Now you can work with JSON, even when you eventually upgrade to PHP 5.2.

 The JSON PEAR Package download page link is given below :
 
   >>> Download the Services_JSON PEAR package


May 6, 2013

Random Password Generator


Random Password Generator

 This tool is used to generate the random password that is generate random string with given "n" length.
This tool is very use full for generate password with full secured.


Password length :
Password :

May 1, 2013

Find Browser and Platform Details Using JavaScript


Find Browser and Platform Details Using JavaScript:

The following code is used to find the Browser and Platform details. This script displays browser code name, browser name, browser version, cookies enabled disabled details, platform details and user agent header details.


<script>
"Browser CodeName: " + navigator.appCodeName + "";
"Browser Name: " + navigator.appName + "";
"Browser Version: " + navigator.appVersion + "";
"Cookies Enabled: " + navigator.cookieEnabled + "";
"Platform: " + navigator.platform + "";
"User-agent header: " + navigator.userAgent + "";
</script>


Apr 7, 2013

Convert php array to javascript array



Convert php array to javascript array

Create php array first,


<?php
$arrPhp=array("1","2","3","4","5');
?>

Create javascript array second


<script>
var arrJava=new Array();
</script>




Now assign php array value to javascript array value


<script>
<?php
for($i=0;$i<count($arrPhp);$i++)
echo "arrJava[".$i."]=".$arrPhp[$i].";";
?>
</script>


the php array now assigned to javascript array.


Remove special charecter using javascript



remove special charecter using javascript:

<input type="text" onkeyup="checkspl(this)";>

/*--- this code in script place ---*/

function checkspl(as)
                {
                     var dd=as.value;
                    if(/^[a-zA-Z0-9- ,]*$/.test(dd) == false)
                    {
                     dd=dd.substring(0,(dd.length-1));
                     as.value = dd;
                    }
                }

This function replace special charecter when type in text field.

Trim spaces from starting and ending of string using javascript



Trim spaces from starting and ending of string using javascript

function removeSpace () 
{
var str=document.getElementById("tex").value;
str[t] = str[t].replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
     if (/\S/.test(str.charAt(i))) {
                      str = str.substring(0, i + 1);
                      break;
                      }
      }
  }
  return str;
            }

Split operators from string in javascript



1. First Method to remove operators:

 '123+456-321'.split(/([-+\/*])/) // gives == ["123", "+", "456", "-", "321"] 

2. Second Method to remove operators:


<script type="text/javascript">
var strIn = '123+456-321';
var tarrN = strIn.split(/\D/);
var tarrO = strIn.split(/\+|\-|\*|\//);
alert(tarrN.join('\n')+'\n\n'+tarrO.join('\n'));
</script>

Mar 3, 2013

Javascript to create zip file




 The Zip file format is very useful for compress the any type of data. we can generate the zip file by using some programming language.
Let us see how to create zip file by using javascript.

1. First we need javascript package jszip.

2. import jszip package in HTML document.

3 use the below code :
      <script type="text/javascript" src="https://raw.github.com/Stuk/jszip/master/jszip.js"></script>
4. Now generate ZIP file like ,

<html>
<head>
    <script type="text/javascript" src="jszip.js"></script>

<script>
 function create_zip() {
    var zip = new JSZip();
    zip.add("hello1.txt", "Hello First World\n");
    zip.add("hello2.txt", "Hello Second World\n");
    content = zip.generate();
    location.href="data:application/zip;base64," + content;
}

</script>

</head>

<body>
     <a href="" onclick="create_zip()">Create Zip</a>
 </body>
</html>


The function create_zip() calls jszip API to generate ZIP file


Feb 23, 2013

JavaScript to find Mouse cursor X and Y axis value


In javascript we can find the mouse cursor points - the coordinate of the mouse pointer by using the following javascript function.

$().mousemove(function(e){
$('div').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
});
</script>
<div>
</div>

The above function display the x and y axis values inside the div element.
In some browser need jquery.js file .. This file is available in many websites.