search

Google
 

Thursday, January 31, 2008

Difference between ReadOnly and Constants

Difference between ReadOnly and Constants

-> A constant is initialized at the time of its creation i.e Compile Time.

-> A ReadOnly can be assigned once in the class constructor allowing you to pass the value at Runtime.


Advantages of declaring Constant

->Declaring fields as constants protects from changing the value accidentally.

->Using constants we perform some optimization by not declaring any stack space for that field.


Tuesday, January 8, 2008

Validating EMail Id - Java Script Code

Validating EMail ID

Some times we want users to enter their EMail Id and we require it to validate.
The following code in java script will help you out.

Add a TextBox to enter email id and a Button (which has to be clicked by the user after entering email address) in .aspx page.

In the given example txtEmail is the id of the text box and btnEmailCheckOut is the id of Button.

In the Code behind .aspx.cs page add the below code in Page_Load event-

btnEmailCheckOut.Attributes.Add("onclick", "javascript:return CheckEmail('" + txtEmail.ClientID + "');");

The above code adds attribute to the button so as to execute a javascript function when the button is clicked. JavaScript function CheckEmail takes a parameter txtEmail.ClientID(id of TextBox which will hold Email Id).

Here's the java script function starts

function CheckEmail(ControlID)
{
var str=document.getElementById(ControlID).value;
if (str != "" )
{
var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)

if (str.indexOf(at)==-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}
if (str.indexOf(at)==-1 str.indexOf(at)==0 str.indexOf(at)==lstr)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}
if (str.indexOf(dot)==-1 str.indexOf(dot)==0 str.indexOf(dot)==lstr str.lastIndexOf(dot)==lstr)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}
if (str.indexOf(at,(lat+1))!=-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}
if (str.substring(lat-1,lat)==dot str.substring(lat+1,lat+2)==dot)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}
if (str.indexOf(dot,(lat+2))==-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}
if (str.indexOf(" ")!=-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();
return false;
}
return true;
}
}

Explanation

function CheckEmail(ControlID)
ControlID is the id of TextBox which will hold EMail ID.

var str=document.getElementById(ControlID).value;
document.getElementByID(ControlID) will Returns the reference to the element whose ID is specified.

document.getElementById(ControlID).value give us text entered in the textbox.
So, str will contain Email Id entered by the user in the text box.

if (str != "" )
checks whether str is null or not.

var lat=str.indexOf(at)
str.indexOf(at) gives first index(position) of at(
'@').
lat holds the index.
similarly,

var ldot=str.indexOf(dot)
ldot contains index of dat(' . ');

var lstr=str.length
str.length returns length of the str(EMail Id )


if (str.indexOf(at)==-1)
{
alert("Invalid E-mail ID");
document.getElementById(ControlID).focus();

return false;
}
Above fuctions checks whether
'@' is present in EMail id or not if not it will show an alert message as shown below.


str.indexOf(at) checks for the first occurance of '@' if there is no occurance of '@' returns -1;

EMail Id should definitely contain @ otherwise it not valid.

document.getElementById(ControlID).focus();

focus() method will place cursor into the specified element .TextBox in the current context.

return false; Every function of javascript should return true or false.

If we wont mention return false; statement, function CheckEmail will be called repetedly and alert message will pop up as soon as we have closed early instance of alert message.

if (str.indexOf(at)==-1 str.indexOf(at)==0 str.indexOf(at)==lstr)

Above function will check whether there is any occurances of '@' or not and if ocuurance of '@' is equal to the length of the EMail ID i.e EMail ID ending with '@' which is absurd if, any of the condition is true then EMail Id is not valid.

if (str.indexOf(dot)==-1 str.indexOf(dot)==0 str.indexOf(dot)==lstr str.lastIndexOf(dot)==lstr)

Above function will check the ocurrances dot(' . ') is there or not and checks whether EMail Id is ending with dot or not.

if (str.indexOf(at,(lat+1))!=-1)

checks whether '@' occurs immediately after the first occurance of '@' .

sam@@yahoo.com is the invalid EMail Id.

if (str.substring(lat-1,lat)==dot str.substring(lat+1,lat+2)==dot)

The above statement checks whether the dot is followed immediately before or after at.

e.g. sam.@yahoo.com or sam@.yahoo.com

if (str.indexOf(" ")!=-1)

The above statement checks whether the statement contains any blank space or not.If you want some special character should not be there in your email id then use

if(str.indexOf("SPECIAL CHARACTER ")!=-1)

Query String

Passing Query String in the Source Page

If we want to pass some values to another page it is possible through query string.
Query string is a string which is appended after the URL.
E.g. Response.Redirect( Sample.aspx?name=sam&age=21&sex=female);

In my previous post I have explained that Response.Redirect is used to move from one webpage to another webpage.
Sample.aspx is the destination page to which we want to migrate.
Query string is appended just after the URL, beginning with
“?”

?name=sam&age=21&sex=female is the query string in the above example where Sample.aspx is the URL and name, age, sex are the parameters with their values sam, 21,female respectively .

Two parameters are separated by ‘&’ in the above example name and age are two different parameters and they are separated from ‘&’.
?name=sam&age=21

Value to the parameter is assigned after equal to (“=”).
?name=sam
Here name is parameter and sam is value assigned to the parameter.


Retrieving Query string in the Destination Page

Query string is retrieved by a command Request.QueryString(“parameter”) in asp.net

e.g. Request.QueryString["id"]


The first thing we do is to check if the query string exists in the first place before we start using it. It could look like this:
if (Request.QueryString["id"] != null)
{
// Do something with the querystring
}


The only problem with the above check to see if the query string is null, is that we don't take into consideration if the query string is filled or not.

That could lead to unhandled exceptions in the code. Instead we should check for query strings like this:

if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
// Do something with the querystring
}


So we do the check again more thoroughly:

if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"].Length == 5)
{
// Do something with the querystring
}

Now we know that we get a query string suitable for further processing.
You can then do more precise data type checks using the Parse or Convert method of most value types or by some other logic.