2

This is my label I want to display if the user have left out field before clicking the button. What am I doing wrong because nothing is happening when I click the button.

<asp:Label ID="lblError" runat="server" 
Text="* Please complete all mandatory fields" style="display: none;" >
</asp:Label>

This is the function I call when I click on the button:

function valSubmit(){
    varName = document.form1.txtName.value;
    varSurname = document.form1.txtSurname.value;

    if (varName == "" || varSurname == "") 
    {
     document.getElementById('lblError').style.display = 'inherit'; 

    }
    else
    { 

     .................other code go here...........................
    return true; 
    } 

}

4 Answers 4

4

Why not use the Validation controls? These will give you client and server side validation out of the box - not that I'm lazy or anything... ;-)

Edit for comment:

The RequiredFieldValidator can be set to display a single red asterisk by the side of each control, and a validation summary control could be used BUT that would take up space.

So, it's possible that ASP.Net is renaming your control, so your JS should read:

document.getElementById('<%= lblError.ClientID %>').style.display = 'inherit';

Give that a go...

Personally, I'd still use the Validator controls ;-)

Sign up to request clarification or add additional context in comments.

4 Comments

Space is a problem on my form, it is a very small pop up form.
Incidentally, if space is a premium, you could use a Custom Validator to check the state of each control and the display of the error message would be taken care of by the Custom Validator as well. You would have to write your JS as previously mentioned (the ClientID) and you would also have to write some corresponding server-side validation in case your visitor has turned off JavaScrript - something your OQ will fall down on.
Meant to mention this as well: if your are using the Validation controls, make sure you check Page.IsValid in your code before performing any operations that rely on that valid data, mmm'kay?
Hi Etienne, could you please post the source of the html page that is generated by your ASPX page? I would like to see what is being emitted by the controls and what's being put in your JS.
1

You shouldn't be using lblError as an ID in JavaScript code. Instead you should use:

'<%= lblError.ClientID %>'

Of course this is only possible if you are generating the JavaScript code in the ASP.NET file.

Comments

0

on your desired event use this

document.getElementById('<%= lblError.ClientID %>').style.display = ""; or
document.getElementById('<%= lblError.ClientID %>').style.display = "block"

ok then try this, instead of client side, make it serverside. First set it invisible like , on formload event set invisible using lblEror.visible = false and remove style ="display:none" from html. Then on the desired event/s make it visible and after processing again invisible.

If you want it strictly thorugh js.try this workaround. remove style from asp label. on body onload make it disable from some js function. now on the btn click event make it visible using the method something like this

    function Validate()
    {
         var objLbl = $get('<%=lblError.ClientID%>');
         if (validations fails)
         {
             objLbl.style.display = ""; //displays label
             return false;
         }
         else
         {
             objLbl.style.display="none" //hides label
             return true;
         }
    }
<asp:button id="btnValidate" runat="server" onclientclick="return validate();"/>

Hope this will work

Comments

-1

Take a look at jquery, you can select by classes instead of id's which will never be altered when rendered onto the page (unlike id's)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.