简体   繁体   English

无法正确提交表格

[英]Unable to Submit a Form Properly

I am new to JavaScript and want learning it to my own I have a problem that my alert box is not showing up in the second method of Verify() method was it me who is doing something wrong or something else ! 我是JavaScript的新手,并希望自己学习它我有一个问题,我的警告框没有出现在Verify()方法的第二种方法是我做错了什么或其他什么! please help me Thanks in advance! 请帮助我提前谢谢! my Code: 我的代码:

<html>
    <head>

        <script>
            function Verify(){
                if(!isValidName()){
                    return false;   
                }

                 if(!isValidID()){
                    return false;
                 }
            }

            function isValidName(){
                var fnam=document.getElementById('fname').value;
                var lnam=document.getElementById('lname').value;
                var fcn=fnam.charAt(0);
                var lcn=lnam.charAt(0);

                    if(fnam==""||fnam=="First Name"){
                        alert("Please Enter First Name!");
                        return false;
                    }
                    else if(lnam==""||lnam=="Last Name"){
                        alert("Please Enter Last Name!");
                        return false;
                    }
                    else if(fcn!=fcn.toUpperCase()){
                        alert("Please Use Uppercase for the First Letter in the First Name!");
                        return false;
                    }
                    else if(lcn!=lcn.toUpperCase()){
                        alert("Please Use Uppercase for the First Letter in the Last Name!");
                        return false
                    }
                    else{
                        var fc=0;
                        for(i=0; i<fnam.length; i++){
                            fc+=1;
                            if(fnam.charAt(i)>=0||fnam.charAt(i)<=9){
                                alert("Your First Name Contains an Integer! at index="+fc);
                                return false;
                                break;
                            }
                        }
                        var lc=0;
                        for(j=0; j<lnam.length; j++){
                            lc+=1;
                            if(lnam.charAt(j)>=0||lnam.charAt(j)<=9){
                                alert("Your Last Name Contains an Integer! at index="+lc);
                                return false;
                                break;
                            }
                        }
                    }
            }

            function isValidID(){

                var g=document.getElementById('aaa').value;
                if(g=="abc"){
                    alert("SOMETHING WRONG!");
                    return false;
                }
            }

            function clearLastName(){
                document.getElementById('lname').value="";  
            }

            function clearFirstName(){
                document.getElementById('fname').value="";  
            }
        </script>
            <title>
                This is NIC Form Example 
            </title>
    </head>

    <body>
        <h1>
            This is NIC Form Example!
        </h1>
            <form name="nicform" onsubmit="return Verify()">
                <table border="1">
                    <tr>
                        <td>
                            First Name:
                        </td>
                            <td>
                                <input type="text" id="fname" value="First Name" onClick="clearFirstName()">
                            </td>
                    </tr>
                        <tr>
                            <td>
                                Last Name:
                            </td>
                                <td>
                                    <input type="text" id="lname" value="Last Name" onClick="clearLastName()">
                                </td>
                        </tr>
                            <tr>
                                <td>
                                    Gender:
                                </td>
                                    <td rowspan="2">
                                        Male:   <input type="radio" id="male" name="rad">
                                            <br/>
                                        Female: <input type="radio" id="female" name="rad">
                                    </td>
                            </tr>
                                <tr>
                                </tr>
                            <tr>
                            <td>
                            <input type="text" id="aaa" value="abc" >
                            </td>
                                <td align="center" colspan="2">
                                    <button type="submit">GO</button>
                                </td>
                            </tr>
                </table>
            </form>
    </body>
</html>

the problem is that if the value of field with id 'aaa' left unchanged ie if on submitting the form its value is "abc" it doesn't show up the alert box! 问题是,如果id为'aaa'的字段的值保持不变,即如果在提交表单时其值为“abc”,则它不会显示警告框!

I think never call second method because you are missing the "return true" in both function if every thing is correct. 我认为永远不会调用第二种方法,因为如果每件事都是正确的,你会错过两个函数中的“return true”。 so if you add that it will solve your issue. 所以如果你补充说它会解决你的问题。

If function one is execution correct so it must required to ass return true.Look like below 如果函数1执行正确,则必须要求返回true。如下所示

    function Verify() {
                if (!isValidName()) {
                    return false;
                }

                if (!isValidID()) {
                    return false;
                }
            }

            function isValidName() {
                var fnam = document.getElementById('fname').value;
                var lnam = document.getElementById('lname').value;
                var fcn = fnam.charAt(0);
                var lcn = lnam.charAt(0);

                if (fnam == "" || fnam == "First Name") {
                    alert("Please Enter First Name!");
                    return false;
                }
                else if (lnam == "" || lnam == "Last Name") {
                    alert("Please Enter Last Name!");
                    return false;
                }
                else if (fcn != fcn.toUpperCase()) {
                    alert("Please Use Uppercase for the First Letter in the First Name!");
                    return false;
                }
                else if (lcn != lcn.toUpperCase()) {
                    alert("Please Use Uppercase for the First Letter in the Last Name!");
                    return false
                }
                else {
                    var fc = 0;
                    for (i = 0; i < fnam.length; i++) {
                        fc += 1;
                        if (fnam.charAt(i) >= 0 || fnam.charAt(i) <= 9) {
                            alert("Your First Name Contains an Integer! at index=" + fc);
                            return false;
                            break;
                        }
                    }
                    var lc = 0;
                    for (j = 0; j < lnam.length; j++) {
                        lc += 1;
                        if (lnam.charAt(j) >= 0 || lnam.charAt(j) <= 9) {
                            alert("Your Last Name Contains an Integer! at index=" + lc);
                            return false;
                            break;
                        }
                    }
                }
                return true;
            }

            function isValidID() {

                var g = document.getElementById('aaa').value;
                if (g == "abc") {
                    alert("SOMETHING WRONG!");
                    return false;
                }
                return true;
            }

            function clearLastName() {
                document.getElementById('lname').value = "";
            }

            function clearFirstName() {
                document.getElementById('fname').value = "";
            }

Function Verify() should return true if form is valid. 如果表单有效,函数Verify()应返回true

function Verify(){
            if(!isValidName()){
                return false;   
            }

             if(!isValidID()){
                return false;
             }
            return true;
        }

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

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