Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

How to restrict validation for mandatory fields in javascript when they are hidden on a condition?

first name &last name fields visible in user profile page on one condition, and not visible on another condition. when they are not visible, then also java script validating these fields. How to restrict validation when they are not visible?

thanks in advance.

What I have tried:

Here is my code:

function validateEditPage() {

var fname = '<%=txtFName.ClientID %>';
var lname = '<%=txtLName.ClientID %>';


if (document.getElementById(fname) != null) {
var varFirstName = document.getElementById(fname).value;
if (varFirstName == "") {
ShowErrorMessage('Please Enter First Name');
document.getElementById(fname).value = "";
return false;
}
}
if (document.getElementById(lname) != null) {
var varLName = document.getElementById(lname).value;
if (varLName == "") {

ShowErrorMessage('Please Enter Last Name');
document.getElementById(lname).focus();
return false;
}
}


return true;
}
Posted
Updated 5-Apr-16 22:48pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Apr-16 3:43am    
There is no such thing as "java script". Yes, I understand what you mean, but do you?
—SA

1 solution

using visibility[^] and display[^] css properties you can check it is hidden or not.

try this

function validateEditPage() {
          debugger;

          var fname = document.getElementById(  '<%=txtFName.ClientID %>');
          var lname = document.getElementById( '<%=txtLName.ClientID %>');


          if (fname != null) {
              if (fname.style.visibility != 'hidden' && fname.style.display != 'none')
              if (fname.value == "") {
                  alert('Please Enter First Name');
                  fname.focus();
                  return false;
              }
          }
          if (lname != null) {
              if (lname.style.visibility != 'hidden' && lname.style.display != 'none')
              if (lname.value == "") {
                  alert('Please Enter Last Name');
                  lname.focus();
                  return false;
              }
          }

          return true;
      }
 
Share this answer
 
v2
Comments
Member 10714689 6-Apr-16 6:15am    
Hi..Karthik, thanks for the response. I solved it from codebehind.
Karthik_Mahalingam 6-Apr-16 6:23am    
ok buddy. cool.

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