www.webdeveloper.com
Results 1 to 10 of 10

Thread: My Script validating only first field!

  1. #1
    Join Date
    Jul 2012
    Posts
    5

    My Script validating only first field!

    Hi,

    am writing this sample script for my simple project. I have all text fields only. However my script perfectly validating only first field, even I wrote same validation script for remaining fields.
    For the 2nd field it's not checking for Alphabets and from there onwards it's not checking any validation. ( I have upto 10 fields in my original Script). Pls suggest. I am very desperate for this.


    <!DOCTYPE html>
    <html>
    <body>


    <h3> Send review request to test@something.com:</h3>

    <script Language="JavaScript" type="text/javascript">

    function FormValidator(theForm)
    {

    // check to see if the field is blank
    if (theForm.sname.value == "")
    {
    alert("Sandbox name should not be blank");
    theForm.sname.focus();
    return (false);
    }

    // require at least 3 characters be entered
    if (theForm.sname.value.length < 3)
    {
    alert("Please enter at least 3 characters in the \"Sandbox Name\" field.");
    theForm.sname.focus();
    return (false);
    }

    // allow ONLY Capital Alphabeticals

    var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
    var checkStr = theForm.sname.value;
    var allValid = true;
    for (i = 0; i < checkStr.length; i++)
    {
    ch = checkStr.charAt(i);
    for (j = 0; j < checkOK.length; j++)
    if (ch == checkOK.charAt(j))
    break;
    if (j == checkOK.length)
    {
    allValid = false;
    break;
    }
    }
    if (!allValid)
    {
    alert("Please enter only Capital Alphabetics \"Sandbox Name\" field.");
    theForm.sname.focus();
    return (false);
    }

    if (theForm.plead.value == "")
    {
    alert("Project Lead login field should not be blank");
    theForm.plead.focus();
    return (false);
    }

    var checkOk1= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var checkStr1 = theForm.plead.value;
    var allValid1= true;
    for (i = 0; i < checkStr1.length; i++)
    {
    ch = checkStr1.charAt(i);
    for (j = 0; j < checkOK1.length; j++)
    if (ch == checkOK1.charAt(j))
    break;
    if (j == checkOK1.length)
    {
    allValid1 = false;
    break;
    }
    }
    if (!allValid1)
    {
    alert("Please enter only Alphabets \"Project Lead Login\" field.");
    theForm.sname.focus();
    return (false);
    }

    if (theForm.talias.value == "")
    {
    alert("Team alias should not be blank");
    theForm.talias.focus();
    return (false);
    }

    var checkOk2= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-";
    var checkStr2 = theForm.talias.value;
    var allValid2= true;
    for (i = 0; i < checkStr2.length; i++)
    {
    ch = checkStr2.charAt(i);
    for (j = 0; j < checkOK2.length; j++)
    if (ch == checkOK2.charAt(j))
    break;
    if (j == checkOK2.length)
    {
    allValid2 = false;
    break;
    }
    }
    if (!allValid2)
    {
    alert("Please enter only Alphabets \"Team Alias\" field.");
    theForm.talias.focus();
    return (false);
    }
    return(true);
    }

    </script>

    <form action="MailTo:ynaveen9@something.com" method="post" name="theForm" onsubmit="return FormValidator(this);" >

    <p>
    Sandbox Name:<br />
    <input type="text" name="sname" /><br />
    Project Lead<br />
    <input type="text" name="plead" /><br />
    TeamAlias<br />
    <input type="text" name="talias" /><br />

    <input type="Submit" value="Sendmail" name="btn">
    <input type="reset" value="Reset"> </p>
    </form>
    </body>
    </html>

    Thanks a bunch!
    Naveen

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Try using the error console...

  3. #3
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    Check this:
    HTML Code:
    <!DOCTYPE html>
    <html>
    <body>
        <h3>Send review request to test@something.com</h3>
        <form action="mailto:ynaveen9@something.com" method="post" name="theForm" onsubmit="return FormValidator(this);" >
            Sandbox Name: <br/> <input name="sname" /> <br/>
            Project Lead: <br/> <input name="plead" /> <br/>
            TeamAlias: <br/> <input name="talias" /> <br/>
            <input type="submit" value="Send Mail" name="btn">
            <input type="reset" value="Reset">
        </form>
        
        <script>
            // The form validating script should always be placed after the form!
            function FormValidator(theForm) {
                // check to see if the field is blank
                if (theForm.sname.value == "") {
                    alert("Sandbox name should not be blank!");
                    theForm.sname.focus();
                    return(false);
                }
                // require at least 3 characters to be entered
                if (theForm.sname.value.length < 3) {
                    alert("Please enter at least 3 characters in the \"Sandbox Name\" field.");
                    theForm.sname.focus();
                    return (false);
                }
                // allow ONLY Capital Alphabets
                if (!(/^[A-Z ]*$/.test(theForm.sname.value))) { // using "regex" in javascript. Check: "http://stackoverflow.com/questions/8411809/regular-expression-for-upper-case-letter-only-in-javascript"
                    alert("Please enter only Capital Alphabets in the \"Sandbox Name\" field!");
                    theForm.sname.focus();
                    return(false);
                }
                /*var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
                var checkStr = theForm.sname.value;
                var allValid = true;
                for (i=0; i<checkStr.length; i++) {
                    ch = checkStr.charAt(i);
                    for (j = 0; j < checkOK.length; j++) {
                        if (ch == checkOK.charAt(j))
                            break;
                        if (j == checkOK.length) {
                            allValid = false;
                            break;
                        }
                    }
                }
                if (!allValid) {
                    alert("Please enter only Capital Alphabetics \"Sandbox Name\" field.");
                    theForm.sname.focus();
                    return (false);
                }*/
    
                if (theForm.plead.value == "") {
                    alert("\"Project Lead\" login field should not be blank!");
                    theForm.plead.focus();
                    return(false);
                }
                
                if (!(/^[A-Za-z]*$/.test(theForm.plead.value))) {
                    alert("Please enter only Alphabets in the \"Project Lead\" login field.");
                    theForm.plead.focus();
                    return(false);
                }
                /*var checkOk1= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                var checkStr1 = theForm.plead.value;
                var allValid1= true;
                for (i = 0; i < checkStr1.length; i++) {
                    ch = checkStr1.charAt(i);
                    for (j = 0; j < checkOK1.length; j++) {
                        if (ch == checkOK1.charAt(j))
                            break;
                        if (j == checkOK1.length) {
                            allValid1 = false;
                            break;
                        }
                    }
                }
                if (!allValid1) {
                    alert("Please enter only Alphabets \"Project Lead Login\" field.");
                    theForm.sname.focus();
                    return (false);
                }*/
    
                if (theForm.talias.value == "") {
                    alert("\"TeamAlias\" field should not be blank!");
                    theForm.talias.focus();
                    return(false);
                }
                if (!(/^[A-Za-z]*$/.test(theForm.talias.value))) {
                    alert("Please enter only Alphabets in the \"TeamAlias\" field.");
                    theForm.talias.focus();
                    return(false);
                }
                /*var checkOk2= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-";
                var checkStr2 = theForm.talias.value;
                var allValid2= true;
                for (i = 0; i < checkStr2.length; i++) {
                    ch = checkStr2.charAt(i);
                    for (j = 0; j < checkOK2.length; j++) {
                        if (ch == checkOK2.charAt(j))
                            break;
                        if (j == checkOK2.length) {
                            allValid2 = false;
                            break;
                        }
                    }
                }
                if (!allValid2) {
                    alert("Please enter only Alphabets \"Team Alias\" field.");
                    theForm.talias.focus();
                    return (false);
                }*/
                return(true); // returning "true" after validating all the form fields to submit the form!
            }
        </script>
    </body>
    </html>
    Come back to me if you do NOT understand a thing!

    Also, do NOT forget to compare my code with yours to find the errors, to know where your code was outdated etc.

  4. #4
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194

  5. #5
    Join Date
    Jul 2012
    Posts
    5
    Thank u very much sir.. It's working fine for me..

  6. #6
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194

    Thumbs up

    Quote Originally Posted by Bobby9 View Post
    Thank u very much sir.. It's working fine for me..
    You are most welcome!

  7. #7
    Join Date
    Jul 2012
    Posts
    5
    Sorry to come back again!! But have no choice..


    <!DOCTYPE html>
    <html>
    <body>

    <form action="MailTo:ynaveen@something.com" method="post" name="theForm" onsubmit="return FormValidator(this)" >

    Sandbox Name:<br />
    <input type="text" name="sname" /><br />
    TeamAlias: <br />
    <input type="text" name="talias" /><br />
    Project Lead: <br />
    <input type="text" name="plead" /> <br />
    Category:<br />
    <input type="text" name="category" /><br />
    Type:<br/>
    <input type="text" name="type" /><br />
    Item:<br/>
    <input type="text" name="item" /><br />
    QUOTA<br />
    <input type="text" name="quota" /><br />
    SCHEMANAME<br />
    <input type="text" name="scname" /><br />
    TT Ref.<br />
    <input type="text" name="tt" /><br />
    LDAP<br />
    <input type="text" name="ldap" />
    <br /><br />
    <input type="Submit" value="Sendmail" name="btn">
    <input type="reset" value="Reset">
    </form>

    <script type="text/javascript">

    function FormValidator(theForm)
    {

    // check to see if the field is blank
    if (theForm.sname.value == "" )
    {
    alert("Sandboxname is mandatory");
    return (false);
    }

    // require at least 3 characters be entered
    else if (theForm.sname.value.length < 3)
    {
    alert("Please enter at least 3 characters in the \"Sandbox Name\" field.");
    theForm.sname.focus();
    return (false);
    }

    // allow ONLY Capital Alphabets
    if (!(/^[A-Z ]*$/.test(theForm.sname.value)))
    {
    alert("Please enter only Capital Alphabets in the \"Sandbox Name\" field!");
    theForm.sname.focus();
    return(false);
    }

    if (theForm.talias.value == "" )
    {
    alert("TeamAlias is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z-]*$/.test(theForm.talias.value)))
    {
    alert("Please enter only Alphabets in the \"Team alias Name\" field!");
    theForm.talias.focus();
    return(false);
    }

    if (theForm.plead.value == "" )
    {
    alert("Project Lead is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.plead.value)))
    {
    alert("Please enter only Alphabets in the \"Project Lead Name\" field!");
    theForm.plead.focus();
    return(false);
    }

    if (theForm.category.value == "" )
    {
    alert("Category is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.category.value)))
    {
    alert("Please enter only Alphabets in the \"Category \" field!");
    theForm.category.focus();
    return(false);
    }

    if (theForm.type.value == "" )
    {
    alert("type is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.type.value)))
    {
    alert("Please enter only Alphabets in the \"type \" field!");
    theForm.type.focus();
    return(false);
    }
    if (theForm.item.value == "" )
    {
    alert("item is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.item.value)))
    {
    alert("Please enter only Alphabets in the \"item \" field!");
    theForm.item.focus();
    return(false);
    }

    if (theForm.quota.value == "" )
    {
    alert("quota is mandatory");
    return (false);
    }

    if (!(/^[0-9]*$/.test(theForm.quota.value)))
    {
    alert("Please enter only Digits in the \"quota \" field!");
    theForm.quota.focus();
    return(false);
    }

    if (theForm.scname.value == "" )
    {
    alert("Schema name is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.scname.value)))
    {
    alert("Please enter only Alphabets in the \"Schema name \" field!");
    theForm.scname.focus();
    return(false);
    }

    if (theForm.tt.value == "" )
    {
    alert("TT ref. is mandatory");
    return (false);
    }

    if (!(/^[0-9]*$/.test(theForm.tt.value)))
    {
    alert("Please enter only Digits in the \"TT ref. \" field!");
    theForm.tt.focus();
    return(false);
    }

    if (theForm.ldap.value == "" )
    {
    alert("LDAP name is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.ldap.value)))
    {
    alert("Please enter only Alphabets in the \"LDAP name \" field!");
    theForm.ldap.focus();
    return(false);
    }

    return (true);
    }
    </script>
    </body>
    </html>

    This is my entire code which is again failing to validate. I just used same simple Regex type validation for all the fields.. It's bit time consuming to go through this.. But pls do for me..

    Thanks!
    Naveen

  8. #8
    Join Date
    Jul 2012
    Posts
    5
    Again my code is failing which I wrote with simple Regex's when I wrote for multiple fields...


    <!DOCTYPE html>
    <html>
    <body>

    <form action="MailTo:ynaveen@something.com" method="post" name="theForm" onsubmit="return FormValidator(this)" >

    Sandbox Name:<br />
    <input type="text" name="sname" /><br />
    TeamAlias: <br />
    <input type="text" name="talias" /><br />
    Project Lead: <br />
    <input type="text" name="plead" /> <br />
    Category:<br />
    <input type="text" name="category" /><br />
    Type:<br/>
    <input type="text" name="type" /><br />
    Item:<br/>
    <input type="text" name="item" /><br />
    QUOTA<br />
    <input type="text" name="quota" /><br />
    SCHEMANAME<br />
    <input type="text" name="scname" /><br />
    TT Ref.<br />
    <input type="text" name="tt" /><br />
    LDAP<br />
    <input type="text" name="ldap" />
    <br /><br />
    <input type="Submit" value="Sendmail" name="btn">
    <input type="reset" value="Reset">
    </form>

    <script type="text/javascript">

    function FormValidator(theForm)
    {

    // check to see if the field is blank
    if (theForm.sname.value == "" )
    {
    alert("Sandboxname is mandatory");
    return (false);
    }

    // require at least 3 characters be entered
    else if (theForm.sname.value.length < 3)
    {
    alert("Please enter at least 3 characters in the \"Sandbox Name\" field.");
    theForm.sname.focus();
    return (false);
    }

    // allow ONLY Capital Alphabets
    if (!(/^[A-Z ]*$/.test(theForm.sname.value)))
    {
    alert("Please enter only Capital Alphabets in the \"Sandbox Name\" field!");
    theForm.sname.focus();
    return(false);
    }

    if (theForm.talias.value == "" )
    {
    alert("TeamAlias is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z-]*$/.test(theForm.talias.value)))
    {
    alert("Please enter only Alphabets in the \"Team alias Name\" field!");
    theForm.talias.focus();
    return(false);
    }

    if (theForm.plead.value == "" )
    {
    alert("Project Lead is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.plead.value)))
    {
    alert("Please enter only Alphabets in the \"Project Lead Name\" field!");
    theForm.plead.focus();
    return(false);
    }

    if (theForm.category.value == "" )
    {
    alert("Category is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.category.value)))
    {
    alert("Please enter only Alphabets in the \"Category \" field!");
    theForm.category.focus();
    return(false);
    }

    if (theForm.type.value == "" )
    {
    alert("type is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.type.value)))
    {
    alert("Please enter only Alphabets in the \"type \" field!");
    theForm.type.focus();
    return(false);
    }
    if (theForm.item.value == "" )
    {
    alert("item is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.item.value)))
    {
    alert("Please enter only Alphabets in the \"item \" field!");
    theForm.item.focus();
    return(false);
    }

    if (theForm.quota.value == "" )
    {
    alert("quota is mandatory");
    return (false);
    }

    if (!(/^[0-9]*$/.test(theForm.quota.value)))
    {
    alert("Please enter only Digits in the \"quota \" field!");
    theForm.quota.focus();
    return(false);
    }

    if (theForm.scname.value == "" )
    {
    alert("Schema name is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.scname.value)))
    {
    alert("Please enter only Alphabets in the \"Schema name \" field!");
    theForm.scname.focus();
    return(false);
    }

    if (theForm.tt.value == "" )
    {
    alert("TT ref. is mandatory");
    return (false);
    }

    if (!(/^[0-9]*$/.test(theForm.tt.value)))
    {
    alert("Please enter only Digits in the \"TT ref. \" field!");
    theForm.tt.focus();
    return(false);
    }

    if (theForm.ldap.value == "" )
    {
    alert("LDAP name is mandatory");
    return (false);
    }

    if (!(/^[A-Za-z]*$/.test(theForm.ldap.value)))
    {
    alert("Please enter only Alphabets in the \"LDAP name \" field!");
    theForm.ldap.focus();
    return(false);
    }

    return (true);
    }
    </script>
    </body>
    </html>


    Sorry to bug u multiple times. But pls suggest..

    Thanks!
    Naveen

  9. #9
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    There's nothing wrong with it - it works. How does it fail for you?

  10. #10
    Join Date
    Jul 2012
    Posts
    5
    That's what am wondering Sir.. Have no clue.. The Script seems to be perfect.. Not sure wht's gng wrong..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles