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)

No comments:
Post a Comment