Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my java script code for validation.

C#
function emptelephoneno() {
     var elem = document.getElementById('<%=txtfullname.ClientID%>');
     if (elem.value.length == 0) {
         elem.style.border = '1px solid red';
         document.getElementById('nameError').innerHTML = "Please Enter Your First Name";
         return false;
     }
     else {
         elem.style.border = '';
         document.getElementById('nameError').innerHTML = "";
     }
     return true;
 }
      function emptelephoneno1() {
      var elem1 = document.getElementById('<%=TextBox1.ClientID%>');
      if (elem1.value.length == 0) {
          elem1.style.border = '1px solid red';
          document.getElementById('Span1').innerHTML = "Please Enter Your First Name";
          return false;
      }
      else {
          elem1.style.border = '';
          document.getElementById('Span1').innerHTML = "";
      }

      return true;
  }
  </script>



this is my textbox where i want to use this code..

XML
<div class="divRegistration">
   <div class="divleft">
   <label> full name :<span class="spanstar">*</span></label>
   </div>
   <div class="divright">
    <asp:TextBox ID="txtfullname" runat="server" MaxLength="30"></asp:TextBox>
    <span id="nameError" class="spanerror"></span>
   </div>
   </div>

   <div class="divRegistration">
   <div class="divleft">
   <label> full name :<span class="spanstar">*</span></label>
   </div>
   <div class="divright">
    <asp:TextBox ID="txtname" runat="server" MaxLength="30"></asp:TextBox>
    <span id="Span1" class="spanerror"></span>
   </div>
   </div>

   <asp:Button ID="btn" runat="server" text="submit" OnClientClick="return emptelephoneno();return emptelephoneno1();"
        onclick="btn_Click" />
   </div>
   </div>
   </div>



and i am using this code's on my aspx.cs page also like that

C#
this.txtfullname.Attributes.Add("onblur", "javascript:emptelephoneno();");
       this.txtname.Attributes.Add("onblur", "javascript:emptelephoneno1();");



when i click the button first validation is working but second validation is not working so please help me out...
Posted
Updated 19-Nov-12 19:36pm
v2
Comments
pradiprenushe 20-Nov-12 1:29am    
Do this
1.Make entry in first textbox
2.press tab button
3.Make entry in first textbox
4.press tab button

Look for senarion.
You are pressing button directly so onblur for last textbox is not getting called.
amitkumar5734 20-Nov-12 1:34am    
my onblur is working correctly with this code . but it's not working when i click on button.. as i have called the function on button click.
NAPorwal(8015059) 20-Nov-12 1:30am    
have u checked textbox's value? is it showing in javascript?

update ur line "if (elem.value.length == 0)" with below code
if (elem.value.length == 0 || elem.value='')
amitkumar5734 20-Nov-12 1:32am    
what ever the code i have posted here.. it's working when i press onblur.. but it's not working on button click..

Just copy and past it in a test page first its working

Step 1: add it in your aspx page
XML
<div class="divRegistration">
            <div class="divleft">
                <label>
                    full name :<span class="spanstar">*</span></label>
            </div>
            <div class="divright">
                <asp:textbox id="txtfullname" runat="server" maxlength="30"></asp:textbox>
                <span id="nameError" class="spanerror"></span>
            </div>
        </div>
        <div class="divRegistration">
            <div class="divleft">
                <label>
                   Contact No :<span class="spanstar">*</span></label>
            </div>
            <div class="divright">
                <asp:textbox id="txtContactNo" runat="server" maxlength="30"></asp:textbox>
                <span id="Span1" class="spanerror"></span>
            </div>
        </div>
        <asp:button id="btn" runat="server" text="submit" onclientclick="return AllValidation();" OnClick="btn_Click" />

Step 2: in head tag
XML
<script>
    
function AllValidation() 
{
   if(emptelephoneno())
   {
      if(emptelephoneno1())
      {
         return true;
      }
    }
    return false;
}
    
    
function emptelephoneno() 
{
   var elem = document.getElementById('<%=txtfullname.ClientID%>');
   if (elem.value.length == 0) 
   {
       elem.style.border = '1px solid red';
       document.getElementById('nameError').innerHTML = "Please Enter Your First Name";
         
       return false;
   }
   else 
   {
       elem.style.border = '';
       document.getElementById('nameError').innerHTML = "";
   }
   return true;
}

function emptelephoneno1() 
{
    var elem1 = document.getElementById('<%=txtContactNo.ClientID%>');
    if (elem1.value.length == 0) 
    {
        elem1.style.border = '1px solid red';
        document.getElementById('Span1').innerHTML = "Please Enter Your First Name";
        return false;
    }
    else 
    {
        elem1.style.border = '';
        document.getElementById('Span1').innerHTML = "";
    }
    return true;
}
</script>
 
Share this answer
 
v2
Try this
C#
<div class="divRegistration">
   <div class="divleft">
   <label> full name :<span class="spanstar">*</span></label>
   </div>
   <div class="divright">
    <asp:TextBox ID="txtfullname" runat="server" MaxLength="30"></asp:TextBox>
    <span id="nameError" class="spanerror"></span>
   </div>
   </div>
 
   <div class="divRegistration">
   <div class="divleft">
   <label> full name :<span class="spanstar">*</span></label>
   </div>
   <div class="divright">
    <asp:TextBox ID="txtname" runat="server" MaxLength="30"></asp:TextBox>
    <span id="Span1" class="spanerror"></span>
   </div>
   </div>
 
   <asp:Button ID="btn" runat="server" text="submit" onclientclick="return AllValidation();" 
        onclick="btn_Click" />
   </div>
   </div>
   </div>

Add this javascript function
JavaScript
function emptelephoneno() {
     var elem = document.getElementById('<%=txtfullname.ClientID%>');
     if (elem.value.length == 0) {
         elem.style.border = '1px solid red';
         document.getElementById('nameError').innerHTML = "Please Enter Your First Name";
         return false;
     }
     else {
         elem.style.border = '';
         document.getElementById('nameError').innerHTML = "";
     }
     return true;
 }
      function emptelephoneno1() {
      var elem1 = document.getElementById('<%=TextBox1.ClientID%>');
      if (elem1.value.length == 0) {
          elem1.style.border = '1px solid red';
          document.getElementById('Span1').innerHTML = "Please Enter Your First Name";
          return false;
      }
      else {
          elem1.style.border = '';
          document.getElementById('Span1').innerHTML = "";
      }
 
      return true;
  }

function AllValidation() {
if(emptelephoneno())
{
if(emptelephoneno1())
{
return true;
}
}
return false;
}
  </script>
 
Share this answer
 
v3
Comments
amitkumar5734 20-Nov-12 1:48am    
still only first textbox is working second textbox is not working..
amitkumar5734 20-Nov-12 1:51am    
please provide me full code..
pradiprenushe 20-Nov-12 1:58am    
Try updated solution.
NAPorwal(8015059) 20-Nov-12 2:05am    
use the solution its working fine ....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900