0

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;
        }
2
  • You need to show your validation code Commented Oct 11, 2013 at 7:11
  • If you are using Visual Studio, you can simply add Validation Controls to your form. Commented Oct 11, 2013 at 7:14

4 Answers 4

1

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

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

Comments

1

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

Comments

0

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.

2 Comments

can you write you javascript function in your question.
Billy, add this javascript function and add also jQuery plugin. fucntion Validate(){ var name = $("#txtPassword").val(); if(name == ""){ alert("Please enter your name"); return false; } }
0

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;

 });
   });

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.