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
|
No responses found. Be the first to respond and make money from revenue sharing program.
|