Friday, November 21, 2008

JavaScript Validatoin For Basic Controles in ASP.NET

0 comments 11/21/2008
Sign up to receive our site updates!




Thanks for Visiting www.funnydotnet.co.cc

Design

In Coding Page Add Following code in Head Tag

<script type="text/javascript" >

function Validation()

{

//name

document.getElementById ('txtname');

if(name.value=='')

{

alert("Name Can't Blank");

getElementById ('txtname').focus();

return false;

}

//checking for only alphabets and name should be more than 4 char

var reforname=/^[A-Za-z]{4,50}$/;

var phone=document.getElementById('txtname').value;

if(phone.search(reforname))

//if(isNaN(phone))

{

alert("Enter Correct name");

document.getElementById ('txtname').focus();

return false;

}



//accepts only 10 digits

var reforphone=/^\d{10}$/;

var phone=document.getElementById('txtphone').value;

if(phone.search(reforphone))

{

alert("Enter Correct Phone No");

document.getElementById('txtphone').focus();

return false;

}

//Email Validation

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

var phone=document.getElementById('txtemail').value;

if(phone.search(reforemail))

{

alert("Enter Correct email");

document.getElementById('txtemail').focus;

return false;

}

//Dropdown list Validation

var dob=document.getElementById('ddldob');

if(dob.value=="select")

{

alert("select DOB");

return false;

}

//Radio Button Validation Here 'gender' is Group Name Of Radio Button

//if(document.getElementById('rb1').checked==false && document.getElementById ('rb2').checked==false)

if(document.form1.gender[0].checked==false && document.form1.gender[1].checked==false)

{

alert("select Gender");

return false;

}

//checkbox validation

if(document.getElementById('cb1').checked==false&& document.getElementById ('cb2').checked==false)

{

alert("select Alert Modes");

return false;

}

return true;

}





Add the Following Code In Page Load



//When Validation Function return True Then OnClick() is Fired (internally calls javaScript Function)

submit.Attributes.Add("OnClick","return Validation()");





Your Ad Here

0 comments:

Respects for your's Questions & Opinions

  • Friends
  •  

    Recent Posts

    New Comments

    Copyright 2008 All Rights Reserved Tech Tasks Template by Rajesh Kumar Chekuri

    Share
    Me
    Twitter Facebook Google +1 Pinterest Delicious Digg Stumbleupon More

    Connect to TT Facebook Google +1
    Back to Top