Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys,

I'm stuck on one part of my project, the validation. I have an Equipment entry field with text boxes and drop down lists. I've written a javascript that Disables the submit button until something is entered into ONE textbox, but then it just leaves the button open if the other fields are empty.

I want to have the client enter in all the fields then the submit button become enabled.

Here is the Javascript I have written


JavaScript
function Validate() {

      if (document.getElementById("<%=btnSubmit.ClientID%>").disabled == true) {
              var serial = document.getElementById("<%=btnSubmit.ClientID%>");
              serial.disabled = false;
          }

      }


I've 5 text boxes and I want to ensure they all have text in them before the submit becomes enabled.

I have a line for just one box, but I don't know how to do it for many boxes?

C#
SerialNum.Attributes.Add("OnKeyDown", "Validate()");


Would anyone be able to provide help for this? A Switch statement in the page load maybe? and if so HOW do I write it is the most confusing bit. If you could do even 2 text boxes I'd do the rest myself.

Thanks Guys and I hope this made sense and you can help out.
Posted
Comments
Vani Kulkarni 13-Jul-12 5:57am    
Please reply as a comment to that solution given. Do not add your comments as solutions.

The logic here would be to loop through all the input controls and see if values exists for them. If any one of them is still empty, don't enable the submit button.

There can be few ways to do it. If you have just the 'input' control for taking data then just loop through all of them. Or else go control by control.
example:
Using normal Javascript:
JavaScript
function Validate() 
{
   var inputField1 = document.getElementById("<%=inputField1.ClientID%>");
   var inputField2 = document.getElementById("<%=inputField2.ClientID%>");
   if(inputField1.value.length > 0 && inputField2.value.length >  0)
      document.getElementById("<%=btnSubmit.ClientID%>").disabled = false;
   else
      document.getElementById("<%=btnSubmit.ClientID%>").disabled = true;
}


CodeBehind file:
C#
inputField1.Attributes.Add("onblur", "Validate()");
inputField2.Attributes.Add("onblur", "Validate()");


This should do.


P.S.: There is way to make it easy using jQuery but that would need a little knowledge about it.
P.S.2: There are validator controls too that does it but current solution is based on as asked.
 
Share this answer
 
v2
Comments
EuphorialXTC 13-Jul-12 6:00am    
Sorry about comments as solutions, this is my first post on the site, only getting the hang of it. This code works well, but when I enter values into the fields it doesn't enable the btnSubmit again once it's entered
Sandeep Mewara 13-Jul-12 6:12am    
My mistake, I missed 'value' before taking length. Answer updated.
if(inputField1.value.length > 0 && inputField2.value.length > 0)
EuphorialXTC 13-Jul-12 6:35am    
Thank you so much, this solved my problem. Thanks again, I marked it as the solution, is there anywhere I can give you Kudos?
Sandeep Mewara 13-Jul-12 7:17am    
Good to know. :)

Marking it a solution and upvoting 5 to my answer is the max Kudo you can give for it. :)
EuphorialXTC 13-Jul-12 7:23am    
Thank you! I have drop down lists on it but I'll write another Javascript using the same idea! Thanks again!
Instead, why don't you use RequiredFieldValidator for all your textboxes and enable the textbox. As you have mentioned that you need to make sure that textboxes should have values before submitting, it is better to use RequiredFieldValidator. See below links for example:
Required Field Validator[^]
required-field-validator[^]
Required Field Validator[^]
 
Share this answer
 
Comments
Sandeep Mewara 13-Jul-12 6:15am    
Comments from OP:

1.) I appreciate your answer. Is there no way I can do it the way I was doing it as changing it the RequiredFieldValidator will play havoc with my code.
I'd prefer if someone could help with the route I'm on and explain to me rather than firing links at me. Thanks!


2.) The RequiredFieldValidator runs against the server and not the Client side if you get what I mean? that's the only reason I wish to avoid it, I'm a student and still learning the tricks of the trade! The RequiredFieldValidator would suit me perfectly but not my boss
you want to have the client enter in all the fields then the submit button become enabled.
i give you globle idea what you do for this. in javascript you make function like

JavaScript
function validation()
{
 if($("#txt").val()!="" && $("#txt").val()!="" &&  //same all txtbox those you want to validate)
 {
    var serial = document.getElementById("<%=btnSubmit.ClientID%>");
    serial.disabled = false;
 }
}


i hope your problem will solve if not please comment .
 
Share this answer
 
v2

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