Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger html5 form validation on asp.Net LinkButton OnClientClick

I have a webform application

In Master Page I have the form

<body class="tile-1-bg">
<form id="form1" runat="server">

and in my page.aspx

<div class="control-group" runat="server" id="divNome">
    <label for="txtNome" class="control-label">Nome o Ragione Sociale*</label>
    <div class="controls">
        <asp:TextBox runat="server" ID="txtNome" class="form-control input-sm" required></asp:TextBox>
    </div>
</div>
<asp:Linkbutton runat="server" ID="lnkBtnRegistrati" class="btn btn-default btn-lg btn-block" Text="REGISTRATI" OnClientClick="if(checkForm())return true; else return false;" OnClick="lnkBtnRegistrati_Click"></asp:Linkbutton>

I would like to call a Javascript function that simulate Button submit and return false if some validation goes wrong and shows the errors on form fields.

    function checkForm()
    {
         ?
    }

How can I do?

like image 576
Martina Avatar asked Oct 03 '17 13:10

Martina


1 Answers

What you want is to return the result of the checkform , you can handle it's state in checkform method it self:

Aspx code

change from

<asp:Linkbutton runat="server" ID="lnkBtnRegistrati" class="btn btn-default btn-lg btn-block" Text="REGISTRATI" OnClientClick="if(checkForm())return true; else return false;" OnClick="lnkBtnRegistrati_Click"></asp:Linkbutton>

to

    <asp:Linkbutton runat="server" ID="lnkBtnRegistrati" class="btn btn-default  
 btn-lg btn-block" Text="REGISTRATION" OnClientClick="return checkform();"  
 OnClick="lnkBtnRegistrati_Click"></asp:Linkbutton>

In your javascript method:

function checkForm()
    {
        if(true) // your condition to check
       {
            return true;

       }
        else{
              return false;
           } 
    }

Explanation :

In the Markup Onclientclick will return whether your condition met or not , if your conditions are satisfied , then you would be returning as return true vice versa . so in this way you can achieve what you want

like image 183
Tummala Krishna Kishore Avatar answered Oct 06 '22 00:10

Tummala Krishna Kishore