Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
this is my java script code for validation.
 
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..
 
<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
 
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 19-Nov-12 19:21pm
Edited 19-Nov-12 19:36pm
v2
Comments
pradiprenushe at 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 at 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.
Neelam Chandra (8015059) at 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 at 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..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this
<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
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>
 
  Permalink  
v3
Comments
amitkumar5734 at 20-Nov-12 1:48am
   
still only first textbox is working second textbox is not working..
amitkumar5734 at 20-Nov-12 1:51am
   
please provide me full code..
pradiprenushe at 20-Nov-12 1:58am
   
Try updated solution.
Neelam Chandra (8015059) at 20-Nov-12 2:05am
   
use the solution its working fine ....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Just copy and past it in a test page first its working
 
Step 1: add it in your aspx page
<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
<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>
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 525
1 CPallini 480
2 George Jonsson 353
3 OriginalGriff 260
4 Pheonyx 218
0 OriginalGriff 4,813
1 CPallini 3,890
2 Sergey Alexandrovich Kryukov 3,274
3 George Jonsson 2,552
4 Gihan Liyanage 2,186


Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100