C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Client Side validations for Text Boxes against white spaces, Date, Email


Posted Date: 03 Oct 2008    Resource Type: Articles    Category: Web Applications

Posted By: Sidewinder2       Member Level: Gold
Rating:     Points: 10



Text box validations for date,white spaces,Email.

For validating a text box against Email, white spaces , date i am going to use jscript functions. This allows the validations are done on client side to save time and resources.

For this we have to specify the location of the jscript file in the aspx page because i am going to use a seperate file which consists of jscript functions only.


To specify the location of the jscript file follow the step below.



Just add
language="jscript" type ="text/jscript" src ="./Jscript.js"
in between the Script tag. the src element specifies the location of the jscript file

and also just drag and drop three text boxes and three lables from the design view in to the web form.



then in the code behind specify the jscript functions that should be fired when the text in the text boxes changes.


TextBox1.Attributes.Add("OnChange", "return EmptyFieldValidation('" + TextBox1.ClientID + "','Name');");
TextBox2.Attributes.Add("OnChange", "return DateValidation('" + TextBox2.ClientID + "','Date');");
TextBox3.Attributes.Add("OnChange", "return EmailValidation('" + TextBox3.ClientID + "','Email Address');");



then in the jscript file add the functions below




function EmptyFieldValidation(ctrl1,type1)
{

var str = document.getElementById(ctrl1).value;


if (str.length<1||!str.match(/[^\s]/))
{

alert("Please Enter"+" "+type1);

document.getElementById (ctrl1).focus();

document.getElementById (ctrl1).value="";

return false;
}

return true;
}



function DateValidation(ctrl2,type2)
{
var regExp = /^([0]?[1-9]|[1][0-2])[/]([0]?[1-9]|[1|2][0-9]|[3][0|1])[/]([0-9]{4}|[0-9]{2})$/;
var str = document.getElementById (ctrl2).value;

if(!str.match(regExp))
{
alert("Please Enter "+" "+type2+" "+"With MM/DD/YY Format");
document.getElementById (ctrl2).focus();
document.getElementById (ctrl2).value ="";
return false;
}
return true;
}



function EmailValidation(ctrl3,type3)
{

var emailPat =/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var emailid=document.getElementById(ctrl3).value;
var matchArray = emailid.match(emailPat);

if (matchArray == null)
{
alert("Invalid"+" "+type3+" "+"! Please re-enter.");
document.getElementById(ctrl3).focus();
document.getElementById(ctrl3).value="";
return false;

}
return true;
}





Thats All The text boxes will be validated at client side when the text in them changes

If you want to validate all the three text boxes when a button is clicked, then in the code behind add the folowing code



SaveButton.Attributes.Add("OnClick", "return wholevalidation('" + TextBox1.ClientID + "','Name','" + TextBox2.ClientID + "','Date','" + TextBox3.ClientID + "','Email Address');");






then in the jscript file add the functions below




function wholevalidation(ctrl11,type11,ctrl22,type22,ctrl33,type33)
{

var v1 = EmptyFieldValidation(ctrl11,type11);

if (v1 == true)
{

var v2 = DateValidation(ctrl22,type22);

if (v2 == true)
{

var v3 = EmailValidation(ctrl33,type33);

if(v3==true)
{


return true;


}
return false;
}
return false;
}
return false;
}


function EmptyFieldValidation(ctrl1,type1)
{

var str = document.getElementById(ctrl1).value;


if (str.length<1||!str.match(/[^\s]/))
{

alert("Please Enter"+" "+type1);

document.getElementById (ctrl1).focus();

document.getElementById (ctrl1).value="";

return false;
}

return true;
}



function DateValidation(ctrl2,type2)
{
var regExp = /^([0]?[1-9]|[1][0-2])[/]([0]?[1-9]|[1|2][0-9]|[3][0|1])[/]([0-9]{4}|[0-9]{2})$/;
var str = document.getElementById (ctrl2).value;

if(!str.match(regExp))
{
alert("Please Enter "+" "+type2+" "+"With MM/DD/YY Format");
document.getElementById (ctrl2).focus();
document.getElementById (ctrl2).value ="";
return false;
}
return true;
}



function EmailValidation(ctrl3,type3)
{

var emailPat =/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var emailid=document.getElementById(ctrl3).value;
var matchArray = emailid.match(emailPat);

if (matchArray == null)
{
alert("Invalid"+" "+type3+" "+"! Please re-enter.");
document.getElementById(ctrl3).focus();
document.getElementById(ctrl3).value="";
return false;

}
return true;
}







Cheers,
Myself






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Email  .  Date  .  Client Side validations for Text Boxes against white spaces  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sharepoint prompts for Username/Password on clicking video files to open in Windows media player.
Previous Resource: How to Check all the check boxes in header
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use