+ Reply to Thread
Results 1 to 5 of 5

Thread: Javascript Validation

  1. #1
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Javascript Validation

    I am writing a validation script and I need it not to go to the page which it would if the form was correct when it isn't. I think it has something to do with the action attribute of the form element, however, I've already used return false

    JS:
    Code:
    function validateForm(help) {
        var 
            inputs = document.forms[0].elements,
            errors=[],
            help = document.getElementById(help);
        for(var i=0;i>inputs.length;i++) {
            if(inputs[i].value.length == 0) {
                errors.push("Invalid Value:"+this.name);
            }
        }
        if( errors.length > 0 
            || inputs[2] != inputs[3] 
            || inputs[4] != inputs[5]
            || inputs[6].checked == false 
            && inputs[7].checked == false) {
    
            inputs[10].disabled = true;
            help.innerHTML = "Either your form is incomplete or there is a problem. "+errors;
            return false;
    
        } else {
            inputs[10].disabled = false;
            help.innerHTML = "";
            document.forms[0].submit();
            return true;
        }
    }
    HTML form:
    HTML Code:
    <form action="index.php" method="post" enctype="multipart/form-data" id="register" name="register">
    <table border="0">
        <tr>
            <td>Username:</td>
            <td><input type="text" name="username" size="12" /></td>
        </tr><tr>
            <td>Password:</td>
            <td><input type="password" name="password" size="12" /></td>
        </tr><tr>
            <td>Confirm Password:</td>
            <td><input type="password" name="confirmEmail" size="12" /></td>
        </tr><tr>
            <td>Email:</td>
            <td><input type="text" name="email" size="40" /></td>
        </tr><tr>
            <td>Confirm Email:</td>
            <td><input type="text" name="confirmEmail" size="40" /></td>
        </tr><tr>
            <td>Gender:</td>
            <td>
                <table border="0">
                    <tr>
                        <td><label><input type="radio" name="gender" value="0" />Male</label></td>
                        <td><label><input type="radio" name="gender" value="1" />Female</label></td>
                    </tr>
                </table>
            </td>
        </tr><tr>
            <td>Location:</td>
            <td><input type="text" name="location" size="50" /></td>
        </tr><tr>
            <td>Avatar:</td>
            <td><input type="file" name="avatar" /></td>
        </tr><tr>
            <td colspan="2"><input type="submit" value="Register" /><input type="hidden" name="page" value="users/addUser" /></td>
        </tr>
    </table>
    </form>
    <span id="help"></span>
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Javascript Validation

    Is the script actually executing? It looks to me like you have a load of newlines that shouldn't be there. I haven't bothered looking at the rest for the moment, but try this first:
    function validateForm(help) {
    var inputs = document.forms[0].elements,
    errors= new Array(),
    help = document.getElementById('help');
    for(var i=0;i>inputs.length;i++) {
    if(inputs[i].value.length == 0) {
    errors.push("Invalid Value:"+this.name);
    }
    }
    if(errors.length > 0 || inputs[2] != inputs[3] || inputs[4] != inputs[5] || inputs[6].checked == false && inputs[7].checked == false) {

    inputs[10].disabled = true;
    help.innerHTML = "Either your form is incomplete or there is a problem. "+errors;
    return false;

    } else {
    inputs[10].disabled = false;
    help.innerHTML = "";
    document.forms[0].submit();
    return true;
    }
    }

  3. #3
    dlukin is offline x10 Lieutenant dlukin is on a distinguished road
    Join Date
    Oct 2009
    Posts
    427

    Re: Javascript Validation

    Where do you attach the function to the form?

  4. #4
    as4s1n's Avatar
    as4s1n is offline x10 Sophmore as4s1n is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    Washington State
    Posts
    174

    Re: Javascript Validation

    Sorry, it was a copy error. The register button has an onclick event with that. It still doesn't work.
    Last edited by as4s1n; 06-21-2010 at 02:13 PM.
    There is no such thing as a "stupid question," there are only "stupid people" who don't ask them.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript Validation

    You also need to hook the form's submit event. If you're using inline registration, it needs to return the function's result.

    HTML Code:
    <form onsubmit="return validate(this);" ...
    If you're using DOM registration (such as provided by jQuery's event system), you need to call Event.preventDefault in your handler.
    Code:
    // jQuery
    function validate (event) {
        // 'this' is the form
        var inputs = this.elements,
        ...
        if (errors.length) {
            event.preventDefault();
            // return false so that handler can also be used inline
            return false;
        }
        ...
    }
    ...
    form.submit(validator);
    Last edited by misson; 06-21-2010 at 02:39 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 03-15-2010, 11:23 AM
  2. ASP.NET Masterpage and javascript form validation
    By yannhuet in forum Programming Help
    Replies: 0
    Last Post: 08-19-2009, 05:21 AM
  3. JavaScript Form Validation
    By kbjradmin in forum Programming Help
    Replies: 7
    Last Post: 06-03-2009, 11:46 AM
  4. form validation w/Javascript
    By surreal5335 in forum Programming Help
    Replies: 3
    Last Post: 05-19-2008, 04:25 AM
  5. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers