Creating ASP label in Javascript?

This is my shortcut that I want to display if the user left the field before clicking the button. What am I doing wrong because nothing happens when I press the button.

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

This is the function that I call when I click 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; 
    } 

}

+3
source share
4 answers

Why not use validation elements? This will give you a check on the client and server side outside the box - not that I'm lazy or something else ...; -)

Edit Comment:

RequiredFieldValidator can be configured to display a single red star next to each control, and a validation check adder can be used, but this will take up space.

, , ASP.Net , JS :

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

...

Validator; -)

+5

lblError JavaScript. :

'<%= lblError.ClientID %>'

, , JavaScript ASP.NET.

+2

jquery, id, ( id)

0

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

ok, . , , formload invisible, lblEror.visible = false html style = "display: none". /s .

, , . asp. js. btn -

    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();"/>

,

0

Source: https://habr.com/ru/post/1727488/


All Articles