Java Nerd
Java Nerd

Reputation: 968

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 ! 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 i.e if on submitting the form its value is "abc" it doesn't show up the alert box!

Upvotes: 0

Views: 47

Answers (2)

Pragnesh Khalas
Pragnesh Khalas

Reputation: 2898

I think never call second method because you are missing the "return true" in both function if every thing is correct. 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

    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 = "";
            }

Upvotes: 2

PiotrHyrczak
PiotrHyrczak

Reputation: 91

Function Verify() should return true if form is valid.

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

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

Upvotes: 0

Related Questions