简体   繁体   English

需要帮助使用java脚本登录验证

[英]Need help login validation using java script

I have problem with login validation using javascript. 我使用javascript登录验证有问题。 When the user doesn't provide any username or password the pop up message is showing please enter the username and password. 当用户未提供任何用户名或密码时,弹出消息显示请输入用户名和密码。 I want that when user doesn't provide username the pop up message will come please enter the username, then if i press OK button cursor should go in txtusername textbox. 我希望当用户没有提供用户名弹出消息时,请输入用户名,然后如果我按OK按钮光标应该进入txtusername文本框。 In case of password also same thing should happen, cursor should go in txtpassword textbox. 在密码的情况下也应该发生相同的事情,光标应该进入txtpassword文本框。 But in my programming the pop up message is coming but after pressing ok button cursor is not placing in expected position, please somebody modify my code. 但是在我的编程中弹出消息即将到来,但是按下ok按钮后光标没有放在预期的位置,请有人修改我的代码。 Here is my code. 这是我的代码。

<script type="text/javascript">
    //Function to check valid login
    function validLogin() {
        if (document.getElementById("TxtName").value == "") {
         alert("Please enter your UserName");
         return false;             
        }
        if (document.getElementById("Txtpassword").value == "") {
           alert("Please enter your Password");
         return false;
        }
        return true;
    }

<asp:Button ID="BtnLogin" runat="server" Text="Login" onclick="BtnLogin_Click" OnClientClick="validLogin()"/>

Give this a try (assuming you meant TxtName, as in your code, not TxtUsername, as in your question. 尝试一下(假设您的意思是TxtName,就像在您的代码中一样,而不是TxtUsername,就像您的问题一样)。

<script type="text/javascript">
    //Function to check valid login
    function validLogin() {
        var name = document.getElementById("TxtName");
        if (name.value == "") {
           alert("Please enter your UserName");
           name.focus();
           return false;             
        }
        var password = document.getElementById("Txtpassword");
        if (password.value == "") {
           alert("Please enter your Password");
           password.focus();
           return false;
        }
        return true;
    }
</script>

use element.focus() after your alert. 警报后使用element.focus()。 ie

alert("please enter your UserName");
document.getElementById("UserName").focus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM