Email validator : Javascript

Here goes the javascript function to check whether the entered text 
is in the format of email. 
A good one.



function validate()

{

  if (echeck(document.getElementById(“email”).value)==false)
  {
    alert(“Please enter a valid e-mail address”);

    document.getElementById(“email”).focus();

    return false;
  }
return true;
}

function echeck(str)
{
  var at=”@”
  var dot=”.”
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1)
  {
    return false
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
  {
    return false
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
  {
    return false
  }

  if (str.indexOf(at,(lat+1))!=-1)
  {
    return false
  }

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
  {
    return false
  }

  if (str.indexOf(dot,(lat+2))==-1)
  {
    return false
  }

  if (str.indexOf(” “)!=-1)
  {
    return false
  }

  if(lstr-ldot==1)
  {
    return false
  }

  return true
}

Add to Bookmark/Favourites : Javascript

As we know, every browsers comes with a functionality to Bookmark/Favourite the current page we are visiting. But most of the users not even think about this feature. Sometimes we can set a link or button or any other control in your mind, in order to remind the user to add the page in their Bookmark/Favourite list. Here goes a simple javascript function that will add the current page in to the users Bookmark/Favourite list.

Use this javascript inside your <Head> tag.


<script language=”javascript” type=”text/javascript”>

function AddFavorite_onclick()
{
    BookmarkTitle = “Silent Killer”;
    BookmarkURL = “https://binzthomas.wordpress.com/&#8221;;

if (window.sidebar)
{ // Bookmark for Mozilla Firefox
    window.sidebar.addPanel(BookmarkTitle, BookmarkURL,””);
}
else if( window.external )
{ // IE’s Favorite
    window.external.AddFavorite( BookmarkURL, BookmarkTitle);
}
else if(window.opera && window.print)
{ // Opera Hotlist
    var element = document.createElement(“a”);
    element.setAttribute(“href”, BookmarkURL);
    element.setAttribute(“title”, BookmarkTitle);
    element.setAttribute(“rel”, “sidebar”);
    element.click();
}
}
</script>

Now add the <a> tag inside the <body> . We are going to add the link text dynamically according to the browser we are using. That means, in IE we have Favourites and in Mozilla it is Bookmarks. So the link will display Add to Favorites in IE and Bookmark Page in Mozilla.

<a href=”javascript:AddFavorite_onclick() ” id=’link’ ></a>


<script language=”javascript” type=”text/javascript”>

if (document.all)
{   
    document.getElementById(‘link’).innerHTML=”Add to Favorites”;
}
else
if (window.sidebar) 

    document.getElementById(‘link’).innerHTML=”Bookmark Page”;   
}
else
if (window.opera && window.print)

    document.getElementById(‘link’).innerHTML=”Add Bookmark”; 
}

</script>

Assigning MasterPageFile Dynamically

If you want to assign the master page for a particular page according to some condition, then here is a simple way to do that.

protected override void OnPreInit(EventArgs e)
{
if (condition)
MasterPageFile = “mstrPage1.master”;
else
MasterPageFile = “mstrPage2.master”;
base.OnPreInit(e);
}