27
May 08

Technical site reviews… What do you think?

I am planning to start a blog (review.techtamasha.com) where I review all the other good technical sites, blogs and all kinds of softwares that will help you make your job as a developer or blogger a lot easier. I’ll keep you posted when I start it and it’s gonna be soon ;o)


06
May 08

Identify the browser used by the client

You may want to know the details of the browser used by the various visitors to your site. Lets see how the navigator object can be used to identify the browser details of the user. The navigator object has a property named ‘appName’ which will do the trick.The code below is all that is needed:

navigator.appName

For Ex.

function identifyBrowser()

{

alert(“You seem to be working on the “+navigator.appName+” browser”);

}

The navigator method has a few other properties like:

appCodeName

appVersion

language

mimeTypes[]

platform

plugins[]

userAgent

Click here to view the example


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})+$/;