admin管理员组

文章数量:1296351

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 A it will return the focus to the textfield name. For that I'd written this script:

<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.

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 A it will return the focus to the textfield name. For that I'd written this script:

<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.

Share asked Feb 20, 2013 at 7:53 Mohammad FaisalMohammad Faisal 5,95915 gold badges73 silver badges124 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

Just give an id for you form and refer it with document.getElementById('form_id'). Use of name attribute in this context has been deprecated over a decade ago. Also name for input should be something else than "name", rather use username or sth.

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:

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

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

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"):

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);
    }
}

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.

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

本文标签: focus textfield if javascript validation failedStack Overflow