Connecting Tech Pros Worldwide Help | Site Map

Javascript Validation

Member
 
Join Date: Dec 2007
Posts: 44
#1: Oct 14 '09
hi,

i have 20 madatory text boxes in one page.and i want to do validation using javascript.For that i am calling following function in button click.

Expand|Select|Wrap|Line Numbers
  1. function validate()
  2. {
  3.  var obj1 = document.getElementById('<%=txtMovie.ClientID%>').id; 
  4.  
  5.          for(var i=1;i<20;i++)
  6.            {
  7.                  var txtID = "obj" + i;
  8.                var txtBox= document.getElementById(txtID);
  9.  
  10.               if(txtID.value.length == 0)
  11.                 {
  12.                  alert("something");;
  13.                    return false;           
  14.              }
  15.           }    
  16. }
  17.  
But in this line i am not able to get the control
Expand|Select|Wrap|Line Numbers
  1. document.getElementById(txtID)
.

How can i do this??
any help??

Regards,
Veena
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Oct 14 '09

re: Javascript Validation


Show the corresponding HTML. Are you sure the IDs of the text boxes are "obj1", "obj2", etc.?
Member
 
Join Date: Dec 2007
Posts: 44
#3: Oct 14 '09

re: Javascript Validation


hi acoder,

thank u for reply.

IDs of text boxes are different.In the above function i will be declaring obj1 ,obj2 as variables and i will assign ids to it.

Expand|Select|Wrap|Line Numbers
  1. var obj1 = document.getElementById('<%=txtMovie.ClientID%>');            
  2. var obj2 = document.getElementById('<%=txtCMovie.ClientID%>'); 
  3. var obj3 = document.getElementById('<%=txtNieMovie.ClientID%>'); 
  4. var obj4 = document.getElementById('<%=txtRelDate.ClientID%>'); 
  5. var obj5= document.getElementById('<%=txtLimDate.ClientID%>'); 

So i want to find the control in foor loop.

regards
veena
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Oct 14 '09

re: Javascript Validation


In that case, try:
Expand|Select|Wrap|Line Numbers
  1. var txtBox = window["obj" + i];
Member
 
Join Date: Dec 2007
Posts: 44
#5: Oct 14 '09

re: Javascript Validation


I tried with ur code but value is undefined.
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 348
#6: Oct 14 '09

re: Javascript Validation


Why can you get the objects by

Expand|Select|Wrap|Line Numbers
  1. var inpObj = document.getElementsByTagName('input');
  2. for(var i=0;i<inpObj.length;i++)
  3. {
  4.    if(inpObj.type="text"||inpObj.type="TEXT")
  5.     {
  6.          //do Validation
  7.     }
  8. }
Thanks and Regards
Ramanan Kalirajan
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#7: Oct 14 '09

re: Javascript Validation


Quote:

Originally Posted by RamananKalirajan View Post

Expand|Select|Wrap|Line Numbers
  1. if(inpObj.type="text"||inpObj.type="TEXT")

this code won't work as intended...

Expand|Select|Wrap|Line Numbers
  1. for(var i=0;i<inpObj.length;i++)
  2. {
  3.    // note the == and the NodeList variable
  4.    if("text" == inpObj[i].type.toLowerCase())
  5.     {
  6.          //do Validation
  7.     }
  8. }
Member
 
Join Date: Dec 2007
Posts: 44
#8: Oct 14 '09

re: Javascript Validation


But i cant use the above code because i have to display different messages based on text box id.

thanks
veena
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#9: Oct 14 '09

re: Javascript Validation


the do something like
Expand|Select|Wrap|Line Numbers
  1. var message = {
  2.   id_1: "fool",
  3.   id_2: "idiot",
  4.   // and so on
  5. };
  6. // validation
  7. alert(message[inpObj[i].id]);
Reply