Solved

style.display = 'none' makes the error msg not display again

Posted on 2013-01-06
10
276 Views
Last Modified: 2013-01-07
I have a clear button and onClientClick, I call a javascript function. This is what's happening

1. I click the submit button and an error msg is displayed because the textbox is empty
2. I click Clear and the Javascript is called and the error msg is cleared
3. So far so good
5. I click Submit again, and the textbox is empty but this time, the error msg is not displayed.

I think it's because of style.display = 'none' that wont allow the error msg to be displayed again.

Anything else i can use besides style.display ='none'?


<script type="text/javascript">
      function clrCtrl() {

          document.getElementById("<%= ccNumber.ClientID %>").value = "";
          document.getElementById("<%= RequiredFieldValidator4.ClientID %>").style.display = 'none';
         
      }

   </script>

Open in new window

0
Comment
Question by:Camillia
10 Comments
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
'inline' and 'block' are the other two options.  You don't have to use that line at all, you can just leave the display setting alone.  What is it supposed to be doing for you?
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
>>What is it supposed to be doing for you?
style.display = 'none'  clears the erorr msg is the first time. This is good.

in Step 5 above, when I click on submit button again when the textfield is empty...the error msg should show again but it doesnt. I think it's because the first time...style.display = 'none'
does something and the error msg is not displayed again...

I cant use "block'...i wasnt to clear the field but i dont want to clear it forever...
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
'block' is for things like <p> and <div> and 'inline' is for text and <span>.  Has nothing to do with clearing the field, only with how it is displayed.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
No, let me rephrase my question This is the literal I have


And an input field:

<input type="text" maxlength="16"   class="InputFields"  tabindex="1" runat="server" name="ccNumber" id="ccNumber" />

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator4" ControlToValidate="ccNumber" Text="Card Number is required." ErrorMessage="Card Number is required."></asp:RequiredFieldValidator>

Open in new window


And the button that calls the Javascript and the submit button

 <asp:LinkButton ID="btnCancel" CausesValidation="false"  OnClientClick="clrCtrl();return false;" runat="server" Text="Clear"  />

<asp:LinkButton ID="lbSubmit"  OnClick="lbSubmit_OnClick" CausesValidation="true"   runat="server" Text="Submit"   />

Open in new window


1. The first time when I click on the submit button without entering anything in the input box, the fieldvalidator error msg is displayed.

2. I click the clear button and this error is  cleared

3. I click the submit button again without entering anything in the input box (just like step 1), but this time, the fieldvalidator doesnt display the error msg. I think it's because of the style.display ='none' does something to that field. If I remove the javascript...the fieldvalidator's error msg gets displayed the second time (or whenever i click submit without entering anything in the input box)
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 166 total points
Comment Utility
style.display ='none' makes it not display.  It's as simple as that.  'none' means no display.  If you want it to display after doing that, you have to make it either style.display ='inline' or style.display ='block'.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 7

Author Comment

by:Camillia
Comment Utility
do I need if-else in the javascript to display and hide?? i dont think so. For example:
User clicks submit and the error msg is displayed. User clicks clear and the error msg is cleared. User clicks submit again...at this point..the error msg should display again ...how can I change that Javascript to display the error msg? I have to check the value of the input box?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
If you are going to call "clrCtrl()" both times then you do need the if-else and a variable to keep track of the state.  Otherwise, it can be two different functions.
0
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 167 total points
Comment Utility
A better way to do is to uses classes. If you have two classes in the css:

.showmsg {display:inline;}
.hidemsg {display:none;}
Then all you need to do in the script is a toggle:

document.getElementById("<%= ccNumber.ClientID %>").className =
(document.getElementById("<%= ccNumber.ClientID %>").value = "")? 'showmsg' : 'hidemsg';

Cd&
0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 167 total points
Comment Utility
This question sounds like a question I answered a while ago:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27914633.html

You have to use visibility:hidden instead of display:none to hide the error message on RequiredFieldValidators without disabling them.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
Tommy, yes..i think i had seen a solution like that somewhere..now i remember it. I'll try it. Thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now