Results 1 to 3 of 3

Thread: Validate textboxes using event handler registration

  1. #1
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Validate textboxes using event handler registration

    I have a piece of JavaScript code that should validate that a username field is not empty or null and that a telephone field is in the correct format using event handler registration. It seems to be validating fine but if there is an error it still manages to submit.

    HTML

    Code:
    <html>
        <head> 
            <script type = "text/javascript"  src = "js/validator.js" >
            </script>
        </head>
    
        <body>
    
            <form id = "decorForm" action = "">
                <table border = "0">
                    <tr>
                        <th>Username: </th>
                        <td>
                            <input type = "text"  id = "myUserName" size = "15" maxlength = "15"/>
                        </td>
                    </tr>
    
                    <tr>
                        <th>Telephone: </th>
                        <td>
                            <input type = "text" id = "telephone" name = "telephone" size = "15" maxlength = "13"/>
                            <br />
                            (999)999-9999
                        </td>
                    </tr>
    
                    <tr>
                        <td>
                            <input type = "submit" value = "Submit" />
                        </td>
                        <td>
                            <input type = "reset" value = "Reset" />
                        </td>
                    </tr>
    
                </table>
    
            </form>
    
            <script type = "text/javascript"  src = "js/validatorr.js" >
            </script>
    
        </body>
    </html>

    JAVASCRIPT (validator.js)


    Code:
    function chkUser() {
    // Verifies that the UserName field is not empty.
        var myUserName = document.getElementById("myUserName");
        if (myUserName.value == ""  || myUserName.value == null) {
            alert ("Please enter a Username!");
            return false;
        } else {
            return true;
        }
    }
    
    function chkTelephone() {
    // Verifies that the Telephone field value is in the correct format.
        var tel = document.getElementById("telephone");
        var pos = tel.value.search(/^\(\d{3}\)\d{3}-\d{4}$/);
        if (pos != 0) {
            alert ("Please enter telephone number in the following format: (999)999-9999");
            return false;
        } else {
            return true;
        } 
    }
    
    function chkFields() {
    // Verifes all fields and returns boolean to event handler
    // The event handler function
        if (chkUser() && chkTelephone()) {
            return true;
        } else {
            return false;
        }
    }
    JAVASCRIPT (validatorr.js)

    Code:
    //Register the event handlers for validator.js
    document.getElementById("decorForm").onSubmit = chkFields;
    I am trying to use this as an example.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There could also be other problems, but in javascript there is no event onSubmit. It's onsubmit. So before anything else can work or not work, this:

    Code:
    //Register the event handlers for validator.js
    document.getElementById("decorForm").onSubmit = chkFields;
    Needs to be:

    Code:
    //Register the event handlers for validator.js
    document.getElementById("decorForm").onsubmit = chkFields;
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    papote (09-25-2014)

  4. #3
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I can't believe a little case insensitive was driving me nuts.
    Thanks.

Similar Threads

  1. Animated Collapse event handler question
    By colindo in forum Dynamic Drive scripts help
    Replies: 2
    Last Post: 03-09-2012, 06:47 PM
  2. 2 function with submit event handler
    By Yammaski in forum JavaScript
    Replies: 6
    Last Post: 02-18-2011, 07:41 PM
  3. Resolved onMouseout event handler
    By twQ in forum JavaScript
    Replies: 2
    Last Post: 12-31-2009, 04:51 AM
  4. Calling two functions on one event handler
    By MaTaX in forum JavaScript
    Replies: 3
    Last Post: 05-15-2009, 01:24 PM
  5. Onload versus onclick event handler
    By CzechBoy in forum Dynamic Drive scripts help
    Replies: 5
    Last Post: 02-28-2007, 11:44 AM

Bookmarks

Posting Permissions

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