如果 javascript 验证失败,则聚焦文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14974803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 18:32:31  来源:igfitidea点击:

focus textfield if javascript validation failed

javascriptfocus

提问by Mohammad Faisal

I've this code:

我有这个代码:

<html>
    <head>
        <script type="text/javascript">
            function validate(){
                var name=document.frm.name.value;
                if(name.indexOf("A")==0){
                    alert(name);
                }
            }
        </script>
    </head>
    <body>
        <form name="frm" action="test.php">
            Enter name:<input type="text" name="name" onblur="validate()"/>
            Enter e-Mail:<input type="text" name="email" onblur=""/>
            <input type="submit"/>
        </form>
    </body>
</html>

In above code, I'd tried to validate textfields when they lose focus. The script working fine if the name starts with A. But I want if the user enter different name which doesn't start with Ait will return the focus to the textfield name. For that I'd written this script:

在上面的代码中,我试图在失去焦点时验证文本字段。如果名称以A. 但我希望如果用户输入不同的名称,而不是以A它开头的将焦点返回到 textfield name。为此,我编写了这个脚本:

<script type="text/javascript">
    var name = document.frm.name.value;
    if(name.indexOf("A") == 0){
        alert(name);
    }else{
        document.frm.name.focus();
    }
</script>

then it doesn't works.

那么它不起作用。

Anybody could help with that what should I do to request focus of textfield name?
I've only a little knowledge of javascript.

任何人都可以提供帮助,我应该怎么做才能请求 textfield 的焦点name
我对javascript.

回答by Teemu

Just give an idfor you form and refer it with document.getElementById('form_id'). Use of nameattribute in this context has been deprecated over a decade ago. Also namefor inputshould be something else than "name", rather use usernameor sth.

只需给id你一个表格并用document.getElementById('form_id'). name十多年前,在此上下文中使用属性已被弃用。也nameforinput应该是别的东西"name",而不是 useusername或 sth。

HTML:

HTML:

<form id="frm" action="test.php">
    Enter name:<input type="text" name="username" id="username" onblur="validate()"/>
    Enter e-Mail:<input type="text" name="email" id="email" onblur=""/>
</form>

JavaScript:

JavaScript:

function validate(){
    var form = document.getElementById('frm');
    if (form.username.value.indexOf("A") === 0) {
        alert(name);
    } else {
        form.username.focus();
    }
}

Instead of retrieving the idof the form, you can also pass the formto validate()as an argument: onblur="validate(this);". Then use that argument as a formin the eventhandler:

相反检索的idform,你也可以通过formvalidate()作为参数:onblur="validate(this);"。然后将该参数用作form事件处理程序中的 a :

function validate(form){
    if (form.username.value.indexOf("A") === 0) {
        alert(name);
    } else {
        form.username.focus();
    }
}

EDIT

编辑

Focus doesn't seem to work without a delay, you can try this (the inputhas an id="username"):

焦点似乎没有延迟,你可以试试这个(input有一个id="username"):

function focusTo (elm) {
    elm.focus();
    return;
}

function validate(){
    var form = document.getElementById('frm');
    if (form.username.value.indexOf("A") === 0) {
        alert(name);
    } else {
        alert('error');
        setTimeout(function () {focusTo(form.username);}, 10);
    }
}

回答by 999k

modifiy your script like this

像这样修改你的脚本

   <html>
    <head>
        <script type="text/javascript">
            function validate(){
                var name=document.frm.name.value;
                if(name.indexOf("A")==0){
                    alert(name);
                }
            }
        </script>
    </head>
    <body >
        <form id="frm" action="test.php">
            Enter name:<input type="text" name="username" onblur="validate()"/>
            Enter e-Mail:<input type="text" name="email" onblur=""/>
            <input type="submit" onclick="check()"/>
        </form>
        <SCRIPT LANGUAGE="JavaScript">
        <!--
        function validate()
        {
            var name = document.getElementById('frm').username.value;
            if(name.indexOf("A") == 0){
                alert(name);
                        document.getElementById('frm').email.focus();
             }else{
                document.getElementById('frm').username.focus();
            }
        }
        function check()
        {
            var name = document.getElementById('frm').username.value;
            if(name.indexOf("A") == 0){
             }else{
                alert("Please enter a name starting with 'A'");
                     document.getElementById('frm').username.focus();
            }
        }
        //-->
        </SCRIPT>
    </body>
</html>

You want to execute function validate()on event onblur. in the script you have written the code for focusing, but not added it in a function.

您想validate()在 event上执行函数onblur。在脚本中,您编写了用于聚焦的代码,但未将其添加到函数中。

回答by Viks

try this document.frm.username.focus();. i hope it will work.

试试这个document.frm.username.focus();。我希望它会起作用。