How to get random item from JavaScript array

Hi,

Today I have a requirement in project to retrieve a random value from javascript array. We can do this by using different method.

I have used below code for it.

HTML Button:




Here I have called “ShowArrayItem” function on click event.

Javascript Function:


function ShowArrayItem()
{
    alert(items[Math.floor(Math.random() * items.length)]);
}

Here is the javascript static array; you can define it as per your requirement;


var items = Array('sachin', 'sehwag', 'virat', 'yuvraj', 'raina', 'dhoni', 'jadeja');

When you click on button then it will take random value from the javascript items array.

You can also check it online demo from here, random item from JavaScript array

That’s it.

file extension validation in jQuery

Hello Friends,

Recently one of my project require file extension validation. I am using jquery to achive this. So, i have done it in simple way with jquery as follow

$(function ()
{
   $('#btnSubmit').click(function ()
   {
      var ext = ['jpeg', 'jpg', 'png', 'gif'];
      var file = $("file").val();
      if ($.inArray(file.split('.').pop().toLowerCase(), ext) == -1)
      {
          alert ('Only jpg, png and gif are allowed.');
      }
   }
}

Include HTML as follow.



It is work with jQuery js. So, include jQuery js file. Hope it will help someone!!!!

how to call server side button event in jquery dialog

Hi All,

Generally we are using jquery modal dialog for modal popup window in our application.

Sometimes we need to call server side button event from modal popup window and it creates problem for us.

Today I am going to describe how can we call asp.net button event from jquery modal popup window.

lets take an simple example:

Asp.Net Button Code:



Jquery Modal Popup Script:


 

Code Behind Button Event:


protected void Button1_Click(object sender, EventArgs e)
{
    //your button click event code comes here
    //you can do anything here
}

Please note that you need to add jquery modal popup related script reference in your code.

IE11 and __doPostBack is undefined

Hi Friends,

Today I have experienced one strange issue on my project. In my local machine everything is working fine but when I have uploaded it on server then it gives me an error.

I have tested it in other browsers and everything going fine but when I tested it on IE11 then it raise the __dopostback error.

I got one link from microsoft regarding this error http://support.microsoft.com/kb/2836939/en-us.

I have updated .net framework version 4 and it solves my problem.

Hope this will also help you !!!

Checkbox custom validation in asp.net

Hello,

In this post I will explain how to validate ASP.Net Checkbox control using ASP.Net Custom Validator and JavaScript.

Javascript:

function CheckboxValidation(sender, args) {
            if (document.getElementById("<%=YourCheckboxControlID.ClientID %>").checked == true) {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        } 

Checkbox:


asp:CheckBox ID="YourCheckboxControlID" runat="server" />

Custom Validator:



Button:




Here, You can see the I have used ClientValidationFunction property of asp.net CustomValidator. Which is used to validate control by using javascript function. So when I click on the button then it validates the checkbox whether it is is checked or not.

Hope this will help you !!!….

Difference between settimeout and setinterval in javascript

Hi Frineds,

Most of people are confused in javascript timer functions. I got many questions on this.

So today I am going to describe the differences between setInterval and setTimeout, which are both time interval based code execution methods of the JavaScript language.

How does the JavaScript function setTimeout work?
setTimeout() : It is used to execute a certain function at the end of given time.
below is the syntax for this function:

var timerVariable = setTimeout(func|code, delay)

func|code
    Function variable or the string of code to execute.
delay
    The delay in milliseconds, 1000 milliseconds = 1 second.

The execution will be performed once after the specified delay.

For example, the code below fires an alert after three seconds:

function functionName() { 
  alert('JavaScript setTimeout example')
}
setTimeout(functionName, 3000)

How does the JavaScript function setInterval work?
setInterval() : It repeats itself at regular time that is passed to this function.
below is the syntax for this function:

var timerVariable = setInterval(func|code, delay)

func|code
    Function variable or the string of code to execute.
delay
    The delay in milliseconds, 1000 milliseconds = 1 second.

The execution will be performed continuously every time the number of delay.

For example, the code below fires an alert on every three seconds:

function functionName() { 
  alert('JavaScript setInterval example')
}
setInterval(functionName, 3000)

Now the question is; how can we stop the execution which was started by the setTimeout and setInterval functions.

For this, we can use clearTimeout() and clearInterval() functions.

below is the syntax for this functions:

clearTimeout(timerVariable) AND
clearInterval(timerVariable)

Hope you enjoyed this article !!!

How do I get human readable file size through javascript

Hi Friends,

Few days ago, I have write blog on human readable file size through c#.

Today I am going to represent same functionality using javascript.

For the above requirement, I have created one general function.

See the below code:

function GetFileSize(size)
{
     var fileUnits = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
     var fuCount = 0;

     while (size >= 1024)
     {
                fuCount++;
                //size /= 1024;
                size = Math.round(size / 1024)
      }

      return size + fileUnits[fuCount];
}

Put above function in your page header section.

you can call it as like below


var filesize = GetFileSize(file.size);
alert(filesize);

Here, I have passed file size as a parameter.

How to use reCaptcha with c#

Hello all,

Today i want to share example of reCaptcha. Some of us have try to implement reCaptcha with c# but they are facing some issues to implement it. So, i have create one simple example with update panel.

First register your site at recaptcha.net. By clicking on create key you will get your public key and private key. We will need this key to implement our captcha.

Now, we need recaptcha.dll for c#. You will get dll from here or go to resourses in recaptcha site.

We are ready to code in our application. First add below code in your aspx page.

// Add to register captcha
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

// Add in your page html

            
                
Post a Comment

Now, add following code in cs file.

// Add following code for your button btnSubmit_Click event.
recaptcha.Validate();
if (Page.IsValid && recaptcha.IsValid)
{
     // your code here
}
else
{
        divRecaptcha.Visible = true;
        ScriptManager.RegisterClientScriptBlock(
          recaptcha,
          recaptcha.GetType(),
          "recaptcha",
          "Recaptcha._init_options(RecaptchaOptions);"
          + "if ( RecaptchaOptions && \"custom\" == RecaptchaOptions.theme )"
          + "{"
          + "  if ( RecaptchaOptions.custom_theme_widget )"
          + "  {"
          + "    Recaptcha.widget = Recaptcha.$(RecaptchaOptions.custom_theme_widget);"
          + "    Recaptcha.challenge_callback();"
          + "  }"
          + "} else {"
          + "  if ( Recaptcha.widget == null || !document.getElementById(\"recaptcha_widget_div\") )"
          + "  {"
          + "    jQuery(\"#" + divRecaptcha.ClientID + "\").html('
');" + " Recaptcha.widget = Recaptcha.$(\"recaptcha_widget_div\");" + " }" + " Recaptcha.reload();" + " Recaptcha.challenge_callback();" + "}", true ); }

Here, script manager block is for reload the captcha when captcha is incorrect. I have faced many issues to implement captcha like my dialog box is not submited, captcha is submit but invalid captcha enter and reload captcha not done etc…

Use custom validator to validate reCaptcha. Also, reload captcha issue solved by script manager.

How to call server side button event from javascript function

Hello Friends,

Today I am going to describe how to call server side event from client side.

we can do this by different ways like web service and Page Methods, using jquery, using scriptmanger and __dopostback, using javascript etc.

Today I will show you how to call a server-side ASP.NET button event from client-side using JavaScript function.

See the below aspx code;


   
This is test page :

You can see that, I have defined one server side button with onclick event.

Here, I have also defined aspx label and client side button.

I have used client side button event for calling the server side button event; where I have write test message in aspx label.

see below code for aspx code behind button event


protected void btnTest_Click(object sender, EventArgs e)
{
    lblMessage.Text = "Test message";
}

Now how can we call this event from client side function; see the below client side funtion


function TestCall()
{
     var btnTest = document.getElementById("<%= btnTest.ClientID %>");
     btnTest.click();
}

That’s it.

Hope this will help you !!….