Friday, April 1, 2011

Hello All

Welcome to my blog

I had this idea of starting a blog that serves Dot Net doubts from various sources.

Hope it helps the needy!

Wednesday, September 17, 2008

Fun with Javascript


Some basic validations using JavaScript


U might got tired of using server side validation which will take so much time to execute and bore the user, so here is an example of basic validations.

function Validationofusername()
{
//For User Name
var UserName=document.getElementById("<%=txtUserName.ClientID %>").value;
var UserNAmeLen=document.getElementById("<%=txtUserName.ClientID %>").value.length;
if(UserName=="")
{
alert('Enter User Name');
document.getElementById("<%=txtUserName.ClientID %>").focus();
return false;
}
if(UserName.length<4 || UserName.length>20)
{
alert('Min. Length of User Name is 4 and Max. Length 20\n Current Length='+UserNAmeLen);
document.getElementById("<%=txtUserName.ClientID %>").focus();
return false;
}
var ExpUserName=/^[A-Za-z0-9/.@-_$#%&*()]+$/;
if(!UserName.match(ExpUserName))
{
alert('UserName Contains Invalid Characters');
document.getElementById("<%=txtUserName.ClientID%>").focus();
return false;
}
}

Which will checks requiredfield validator and the length should be in the range of 4 to 20 and allows only some special characters


Writing Regular expressions

/^[A-Za-z0-9/.@-_$#%&*()]+$/;

For example take the above case

/ means start from the string

+$/ end of string

[ ] indicates allowble characters

A-Z means all uppercase

a-z all lowercase

0-9 all numerics

Then special chars whatever we want to allow

Caution:

Special chars should be given in sequence like

~!@#$%^&*()_+

Means start from the left to right

Some browsers don’t support + sign so be careful while using that


cheat for your email expression


Just add an regular expression validation and then go for email

There u can see the regular expression ,

Just ctrl+c and ctrl+v

It may look like this

var EmailExp=/^(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)+$/;


website url validation


var WebsiteExp=/^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}[0-9A-Za-z\.\-]*\.[0-9A-Za-z\.\-]*$/;


Phone number

U have to allow only numerics so

Var phoneEXp=/^[0-9]+$/;


For check box validation

if(document.getElementById("<%=chkID.ClientID%>").checked==false)
{
alert('You must Accept Terms and Conditions');
document.getElementById("<%=chkChecked.ClientID%>").focus();
return false;
}


For dropdownlist validation

if(document.getElementById("<%=ddlCity.ClientID%>").selectedIndex==0)
{
alert('Select City');
document.getElementById("<%=ddlCity.ClientID%>").focus();
return false;
}


For listbox validation

if(document.getElementById("<%=lbKeySkills.ClientID%>").options[0].selected)
{
alert('Not allowed to select First option in Key Skills');
document.getElementById("<%=lbKeySkills.ClientID%>").focus();
return false;
}
var i=document.getElementById("<%=lbKeySkills.ClientID%>").options.length;

var Count=0;
for(var j=0;j {
if(document.getElementById("<%=lbKeySkills.ClientID%>").options[j].selected)
{
Count=Count+1;
}
}
if(Count>3)
{
alert('Not allowed to select more than 3 options');
document.getElementById("<%=lbKeySkills.ClientID%>").focus();
return false;
}




For date validation

if(document.getElementById("<%=txtDOB.ClientID %>").value == "")
{
alert('Please Enter Date');
document.getElementById("<%=txtDOB.ClientID %>").focus();
return false;
}
if(document.getElementById("<%=txtDOB.ClientID %>").value!="")
{
var dateStr=document.getElementById("<%=txtDOB.ClientID %>").value;
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null)
{
alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
return false;
}

month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12)
{ // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31)
{
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
alert("Month "+month+" doesn't have 31 days!")
return false;
}

if (month == 2)
{ // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap))
{
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
}




To trim the string from left&right


function TrimString(inputstring) {
inputstring= inputstring.replace( /^\s+/g, "" ); // strip starting
return inputstring.replace( /\s+$/g, "" ); // strip ending
}



Making the first letter as capital of the given string



function InitialCaps(inputstring) {
inputstring= inputstring.toLowerCase();
inputstring= inputstring.substr(0, 1).toUpperCase() + theString.substring(1, inputstring.length);

var i = 0;
var j = 0;
while((j = inputstring.indexOf(" ", i)) && (j != -1)) {
inputstring= inputstring.substring(0, j + 1) + inputstring.substr(j + 1, 1).toUpperCase() + inputstring.substring(j + 2, inputstring.length);
i = j+1;
}
return inputstring;
}