22
May 10

How to dynamically call javascript functions

I’ve had a lot of moments when I needed to call a javascript function dynamically. By dynamic function call in javascript, I mean when you do not know the function name to be called before hand i.e. the function name to be executed is provided at runtime.

All you need to do is – window["functionname"]();

Yeah, that’s all! Here functionname would be replaced with the name of your function. As always, I’ll provide a working example so that there is no confusion as to how to make dynamic function calls in Javascript.


29
Dec 08

Javascript Substring function

Javascript has an inbuilt substring function which helps to extract parts of a string.

substring(start, stop) :

start (mandatory) – The starting index

stop (optional) – The index to stop extraction

Example: If you want to find the first 3 alphabets in the word INDIA, the code would be

var name = “INDIA”;

var firstThreeLetters = name.substring(0,3);

alert(firstThreeLetters);

The alert would display : IND

Note: Here start is 0 hence the extraction starts from the letter in the 0th position i.e. the first letter. The stop index is 3 which means extraction would stop at index 3 (the letter at index 3 will not be extracted)

Since stop is optional, if you only specify the start parameter, all the letters after the starting index (including the one at the start) would be extracted.

Example: Consider the same example as above, for the word INDIA if we were to give the start as 1

var name = “INDIA”;

var extractedLetters = name.substring(1);

alert(extractedLetters);

The alert would display NDIA as the output of the javascript substring method.

————————————————————————————

Indepth Analysis of Javascript substring

Let’s try and create our own Javascript substring function.

String.prototype.mySubstring = function(start,stop) {

var charArray = this.split(‘);

var temp = “”;

for(var i=start;<stop;i++)

{

temp = temp + charArray[i];

}

return temp;

}

Now, you can use this function instead of the default substring function. Include the above code in any page and you can use mySubstring inside any javascript function.

Example:

var name = “INDIA”;

var extractedLetters = name.mySubstring(0,3);

alert(extractedLetters);

The alert would display IND thus emulating the real Javascript substring function!

Note: This method is just a food for thought. To help you understand how exactly such methods would be implemented. For all practical purposes use the default substring function of Javascript.


05
May 08

Javascript email validation

This post will describe the javascript email validation.

First and foremost, all apologies for such a long delay in writing a new post. I was kind of tied up with some work. But from now on I’ll try my best to be regular :)

We will be using regular expression for email validation. And don’t you worry. I’ll see to it that by the time you finish reading this post, you would have got a fair idea about Regular Expressions.

Lets try to construct the regular expression that we will be using to check the email entered by the user.

Consider these two email id’s : example@example.com and exam_ple@ex.am.ple

Quick references:

. (dot) matches any single character. Ex. “h.t” will match “hut”, “hot” etc.

[ ] Brackets match a single character that is contained in the given expression. Ex. [a-d] will match ‘a’ or ‘b’ or ‘c’ or ‘d’. Similarly, [a-cx-z] will match ‘a’ or ‘b’ or ‘c’ or ‘x’ or ‘y’ or ‘z’.

^ Matches the starting position within the given string.

$ Matches the ending position of the given string.

* Matches the preceding element zero or more times.

+ Matches the preceding element one or more times.

{n,m} Matches the occurrence of the previous symbol not less than n and not more than m times.

\ Its the escape character. Ex. As shown above, .(dot) matches a single occurrence of any character. But what if we need to match an occurrence of (dot) itself. We write it as \.

Considering the email id’s shown above, we can list our requirements as follows:

1. Email Id needs to start with alphabets or numbers – regex will be ^([a-zA-Z0-9]) i.e. the email id should start with either an alphabet or a number.

2. It can contain any number of alphabets, numbers and a few special characters. RegEx will be ([a-zA-Z0-9\.\_\-])*

3. Then there should be the famous ‘@’ symbol which we simply write as @

4. Then again any number of alphabets or numbers or ‘-’ are allowed. RegEx is (([a-zA-Z0-9\-])+\.)+

5. Finally, the ending of the email ([a-zA-Z]{2,4})+$

Here’s the final regular expression for validating the email id’s :

^([a-zA-Z0-9])([a-zA-Z0-9\.\_\-])*@(([a-zA-Z0-9])+(\.))+([a-zA-Z]{2,4})+$

Now, lets see how to use it in the javascript. We make use of the search() function of javascript.

function emailValidation(){

var emailRegEx = /^([a-zA-Z0-9])([a-zA-Z0-9\._-])*@(([a-zA-Z0-9])+(\.))+([a-zA-Z]{2,4})+$/ ;

var testMail = “example@example.com”;

if(testMail.search(emailRegEx)==-1)

alert(“Invalid”);

}

Click Here to view the example.

UPDATE 1 : Please use the regular expression shown below instead of the one above. This fixes the bug which allowed consecutive special charaters i.e. previously example..example@example.com was displayed as valid. It has been fixed now. Please do test the script before you plan to use it. If you find some problems in it then do let me know. I’ll try and fix it.

var emailRegEx = /^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._-])?([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/;

UPDATE 2: I’ve changed the regex a bit to allow email with ‘+’ in them. So, test+example@example.com would be a valid email.

var emailRegEx = /^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._\+-])*([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/;