Billy
Billy

Reputation: 905

asp.net javascript validation issue

I'm using asp.net. I have a login form. I have written Javascript validation function for this in Page.

<table>
    <tr>
        <td>Username:></td>
        <td><asp:TextBox runat="server" id="txtUsername"/></td>
        <td>Password:></td>
        <td><asp:TextBox runat="server" id="txtPassword"/></td>
        <td>Username:></td>
        <td><asp:Button runat="server" id="btnLogin" Text="Login" onclientclick="return Validatecp()"/></td>
    </tr>
</table>

When I focus on textbox and press enter, the entire page is reloading. I want to show the Validation on enter button press. Is that possible?

Edit:

Validation Function:

function Validatecp() {

            var name = document.getElementById('<%=txtName.ClientID %>').value;
            var password= document.getElementById('<%=txtTo.ClientID %>').value;

            if (name == "") {
                document.getElementById("td1").innerHTML = "Name Required.";
                return false;
            }
            else {
                document.getElementById("td1").innerHTML = "";
            }
            if (password== "") {
                document.getElementById("td2").innerHTML = "password Required.";
                return false;
            }               
            else {
                document.getElementById("td2").innerHTML = "";
            }
            return true;
        }

Upvotes: 0

Views: 651

Answers (4)

Laurence
Laurence

Reputation: 7823

I would do that in Jquery.

//set this so that Jquery selector can get your button
 <asp:Button runat="server" id="btnLogin" Text="Login" ClientIDMode="Static" />

jquery

$(function) () {

$('#btnLogin').click(e) {

Your javascript code here

 //put those if you don't want your page reloading
 e.preventDefault();
 return false;

 });
   });

Upvotes: 0

शेखर
शेखर

Reputation: 17614

If you have written you java-script code then make sure that you return false from the js code if it is not valid and in aspx file you need to use return as follows

<asp:Button runat="server" id="btnLogin" Text="Login" 
     OnClientClick="return Validate()"/>

Edit -1

There is a chance that your button is not the default one so the page get refreshed when you press the enter button. For this use code below

<asp:Panel ID="p" runat="server" DefaultButton="myButton">
      <%-- Text boxes here --%>
      <asp:Button ID="myButton" runat="server" />
</asp:Panel>

some useful links

  1. http://www.hanselman.com/blog/ASPNETHowToCreateADefaultEnterButtonForFormsPostBacks.aspx
  2. how to set a default 'enter' on a certain button

Upvotes: 1

Ram Singh
Ram Singh

Reputation: 6918

you want to validate the value of textbox when user clicks enter and focus is in textbox then use the below code:

 <table>
        <tr>
            <td>
                Username:>
            </td>
            <td>
                <asp:TextBox CssClass="save" runat="server" ID="txtUsername" />
            </td>
            <td>
                Password:>
            </td>
            <td>
                <asp:TextBox CssClass="save" runat="server" ID="txtPassword" />
            </td>
            <td>
                Username:>
            </td>
            <td>
                <asp:Button CssClass="save" runat="server" ID="btnLogin" Text="Login" />
            </td>
        </tr>
    </table>
    <asp:Button ID="submit" runat="server" Text="submit" />

and jquery code:

<script type="text/javascript">
    $('.save').live('keydown', function (event) {
        if (event.keyCode == '13') {
            alert('heee');
            return false;
            event.preventDefault();
        }
    });
</script>

Or if you want to validate the controls on enter button click only then use the below code:

<asp:Panel ID="pnl" runat="server" DefaultButton="submit">
        <table>
            <tr>
                <td>
                    Username:>
                </td>
                <td>
                    <asp:TextBox CssClass="save" runat="server" ID="txtUsername" />
                </td>
                <td>
                    Password:>
                </td>
                <td>
                    <asp:TextBox CssClass="save" runat="server" ID="txtPassword" />
                </td>
                <td>
                    Username:>
                </td>
                <td>
                    <asp:Button CssClass="save" runat="server" ID="btnLogin" Text="Login" />
                </td>
            </tr>
        </table>
        <asp:Button ID="submit" runat="server" Text="submit" />
    </asp:Panel>

and jquery code is:

 $('[id$=submit]').click(function (event) {
        if (event.keyCode == '13') {
            alert('heee');
            return false;
            event.preventDefault();
        }
    });

that's all

Upvotes: 1

Manas Kumar
Manas Kumar

Reputation: 2521

You need add a add an attribute to button(btnLogin) OnClientClick="Validate()".

Like:

<asp:Button runat="server" id="btnLogin" Text="Login" 
     OnClientClick="Validate()"/>

Define javascript function Validate() and return false if your form value is not valid.

Upvotes: 0

Related Questions