Monday, July 7, 2008

Standard Javascript Validation Coding

Dear All ,

i have list out the Standard Javascript Validation Coding ,I hope it may be usefully for all Web developer .

Example.js

function Validator(frmname)
{
this.formobj=document.forms[frmname];
if(!this.formobj)
{
alert("BUG: couldnot get Form object "+frmname);
return;
}
if(this.formobj.onsubmit)
{
this.formobj.old_onsubmit = this.formobj.onsubmit;
this.formobj.onsubmit=null;
}
else
{
this.formobj.old_onsubmit = null;
}
this.formobj.onsubmit=form_submit_handler;
this.addValidation = add_validation;
this.setAddnlValidationFunction=set_addnl_vfunction;
this.clearAllValidations = clear_all_validations;
}
function set_addnl_vfunction(functionname)
{
this.formobj.addnlvalidation = functionname;
}
function clear_all_validations()
{
for(var itr=0;itr < validationset =" null;" itr="0;itr" str =" var ret = " itemobj =" this.formobj[itemname];" validationset =" new" desc="desc;" error="error;" itemobj =" inputitem;" validate="vdesc_validate;" vset="new" add=" add_validationdesc;" validate=" vset_validate;" itemobj =" inputitem;" itr="0;itr= 0)
{
command = strValidateStr.substring(0,epos);
cmdvalue = strValidateStr.substr(epos+1);
}
else
{
command = strValidateStr;
}
switch(command)
{
case "req":
case "required":
{
if(eval(objValue.value.length) == 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : Required Field";
}//if
alert(strError);
return false;
}//if
break;
}//case required
case "maxlength":
case "maxlen":
{
if(eval(objValue.value.length) > eval(cmdvalue))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : "+cmdvalue+" characters maximum ";
}//if
alert(strError + "\n[Current length = " + objValue.value.length + " ]");
return false;
}//if
break;
}//case maxlen
case "minlength":
case "minlen":
{
if(eval(objValue.value.length) < length ="=" strerror =" objValue.name" length = " + objValue.value.length + " charpos =" objValue.value.search("> 0 && charpos >= 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Only alpha-numeric characters allowed ";
}//if
alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
return false;
}//if
break;
}//case alphanumeric
case "num":
case "numeric":
{
var charpos = objValue.value.search("[^0-9]");
if(objValue.value.length > 0 && charpos >= 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Only digits allowed ";
}//if
alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
return false;
}//if
break;
}//numeric
case "telephone":
{
var charpos = objValue.value.search("[^0-9 - ()]");
if(objValue.value.length > 0 && charpos >= 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Only digits allowed ";
}//if
alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
return false;
}//if
break;
}
case "alphabetic":
case "alpha":
{
var charpos = objValue.value.search("[^A-Za-z]");
if(objValue.value.length > 0 && charpos >= 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Only alphabetic characters allowed ";
}//if
alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
return false;
}//if
break;
}//alpha
case "txtlength":
{

if(objValue.value.length > 0)
{
var test=objValue.name
var test1=objValue.name.replace(/Name/, "Email")

if(document.forms["QubePayPal"][test1].value.length > 0)
{
return true;
}
else
{
alert('Enter the '+test1);
document.forms["QubePayPal"][test1].focus()
return false;

}

return false;
}
break;
}
case "txtlength1":
{

if(objValue.value.length > 0)
{
var test=objValue.name
var test1=objValue.name.replace(/Email/, "Name")

if(document.forms["QubePayPal"][test1].value.length > 0)
{
return true;
}
else
{
alert('Enter the '+test1);
document.forms["QubePayPal"][test1].focus()
return false;

}

return false;
}
break;
}
case "alnumhyphen":
{
var charpos = objValue.value.search("[^A-Za-z0-9\-_]");
if(objValue.value.length > 0 && charpos >= 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _";
}//if
alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
return false;
}//if
break;
}
case "email":
{
if(objValue.value.length!=0)
{
if(!validateEmailv2(objValue.value))
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Enter a valid Email address ";
}//if
alert(strError);
return false;
}//if

}
else
{
return true;
}
break;
}//case email
case "lt":
case "lessthan":
{
if(isNaN(objValue.value))
{
alert(objValue.name+": Should be a number ");
return false;
}//if
if(eval(objValue.value) >= eval(cmdvalue))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : value should be less than "+ cmdvalue;
}//if
alert(strError);
return false;
}//if
break;
}//case lessthan
case "gt":
case "greaterthan":
{
if(isNaN(objValue.value))
{
alert(objValue.name+": Should be a number ");
return false;
}//if
if(eval(objValue.value) <= eval(cmdvalue)) { if(!strError || strError.length ==0) { strError = objValue.name + " : value should be greater than "+ cmdvalue; }//if alert(strError); return false; }//if break; }//case greaterthan case "regexp": { if(!objValue.value.match(cmdvalue)) { if(!strError || strError.length ==0) { strError = objValue.name+": Invalid characters found "; }//if alert(strError); return false; }//if break; }//case regexp case "dontselect": { if(objValue.selectedIndex == null) { alert("BUG: dontselect command for non-select Item"); return false; } if(objValue.selectedIndex == eval(cmdvalue)) { if(!strError || strError.length ==0) { strError = objValue.name+": Please Select one option "; }//if alert(strError); return false; } break; }//case dontselect }//switch return true; } Then We have to add following line in yours Validation page and Include the Example.js file also. var frmvalidator = new Validator("FORM NAME"); frmvalidator.addValidation("FieldTextBoxName","req","Please enter your FieldTextBoxName"); frmvalidator.addValidation("FieldTextBoxName","maxlen=20","Max length for FieldTextBoxName is 20"); frmvalidator.addValidation("FieldTextBoxName","alpha"); the Above Example validate the Alphabets.

Tuesday, July 1, 2008

S.Vinothkumar: RoleManagement in Asp.Net

S.Vinothkumar: RoleManagement in Asp.Net

How to Get Client IP using C#..?

Following Lines are help us to get Client IP using C#.

string myHost = System.Net.Dns.GetHostName();

string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();