Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
<script type="text/javascript" language="javascript" >

      function ValidateField(sender, args) {
         if (document.getElementById("<%= TextBox2.ClientID%>").value == "") {
             alert("Please enter the quantity");
             document.getElementById("<%=TextBox2.ClientID %>").focus();
             return false;
         }

         for (var i = 0; i <= 20; i++)
         {
             var txtbox = document.getElementById("Textbox" + i)

             if (txtbox)
              {

                  if (txtbox.value != null && txtbox.value.trim() == "") 
                 {

                     alert("Serial Number field cannot be empty");

                     return false;

                 }

             }

         }

    }    
     
   </script>

Hello Everyone
In my code above, it is working fine for enter quantity but for enter serial number it is showing the following error
Microsoft JScript runtime error: Object doesn't support this property or method.
Kindly help
Posted
Updated 5-Aug-14 2:25am
v2
Comments
Dilan Shaminda 5-Aug-14 9:23am    
i just tried your code with serial number format like XXXX-XXXX-XXXX-XXXX it works fine.No errors.What is your serial number format?
Dilan Shaminda 5-Aug-14 9:27am    
and what is if (txtbox) ?
Sergey Alexandrovich Kryukov 5-Aug-14 18:51pm    
The problem has nothing to do with ASP.NET, this is Javascript.
This runtime error is related to the object itself, not data you enter. You are messing up something in your report of this problem. On what property or method of what object do you face it? Can you use the debugger to pin-point it?
—SA
Member 10578683 6-Aug-14 1:48am    
Hello

for (var i = 0; i <= 20; i++) {
var txtbox = document.getElementById("Textbox" + i)

if (txtbox) {



if ( txtbox.value == "") {

alert("Serial Number field cannot be empty");

return false;

}
I wrote the code like above. It is working fine but problem is it is taking space. how to trim the text box. when i write Trim() it is showing error. please help

The same situation occured with me.Yoy Might be using IE.so basically place your javascript code in the end or if you are using jquery then under $(document).ready.

Before Detecting value detect whther it is not null or not an object.
Place your code in try catch block

JavaScript
try{
--;
}
catch(er){
throw ex;
}


Refer the following link
MSDN Microsfot
W3 schools
 
Share this answer
 
v2
Please see this discussion on checking up the values returned by jQuery: http://stackoverflow.com/questions/3247251/is-ifdocument-getelementbyidsomething-null-identical-to-ifdocument-getel[^].

At the same time, there is no need to checkup txtbox.value != null for any really existing text box: it can be empty string, not null.

—SA
 
Share this answer
 

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