SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    Leeds, U.K.
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple array in form validation problem

    hey, I have an array,and id like to use it in my form validation, pretty much:

    Code:
    if (document.register.checkArray[0][0].value = "") {
    but I get this error:

    "Error: form.checkArray has no properties"

    I can alert checkArray[1][0], which I get email (correct)

    I've also tried setting it to a variable, and using that, with no luck.

    so how can I include this in my check?

    Thanks

  2. #2
    SitePoint Member vidalstat's Avatar
    Join Date
    Dec 2007
    Location
    Brooklyn NY
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validation check

    This may not be exactly what you're looking for, but when testing a condition in an if statement you must use the == operator to test for ex.

    Code:
    if(var == ""){
     // code if condition is true
    }
    if you do it it like this:

    Code:
    if(var = ""){
    // cod if condition is true
    }
    your actually just testing if assigning "" to var is true not whether the actual variable is true

    hope this helps

  3. #3
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to post all your code.


    Like vidalstat hinted at

    if (document.register.checkArray[0][0].value = "") {

    is wrong, you have to use

    if (document.register.checkArray[0][0].value == "") {

  4. #4
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    Leeds, U.K.
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, i typed it out rather than c&p, it was actually "==", not "=".

    here is all the revevant code:

    Code:
    form = document.register;
    	
    	var checkArray = new Array(3)
    	for (i=0; i <=2; i++){
    		checkArray[i] = new Array(2);
    	}
    	checkArray[0][0] = "password";
    	checkArray[0][1] = "passError";
    	checkArray[1][0] = "password2";
    	checkArray[1][1] = "pass2Error";
    	checkArray[2][0] = "email";
    	checkArray[2][1] = "emailError";
    	alert(checkArray[2][0]);
    	for (i=0;i<=checkArray.length;i++){
    		
    		if (form.checkArray[i][0].value == ""){
    			document.getElementById(checkArray[i][1]).innerHTML = "Missing Field";
    			document.register.sendReg.disabled = false;
    			return;
    		}
    		
    	}

  5. #5
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks like a complicated way for solving a simple problem.

    If you can post your form structure and how you want it validated I'm sure we can come up with something better.

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    Leeds, U.K.
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ive already got the validation working , just using if statements, just thought I'd make this, then if I have a huge form, all I have to do is update the array lol.

    the only problem really is how to link variables, like the:

    Code:
    form.checkArray[i][0].value == ""
    i know in php, the "." works fine, and in javascript you usually use "+", but I can get neither to work. checkArray[0][0] definiately equals the right thing ("password") when I alert it, but how would I get to use that in the method above?

  7. #7
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Make sure that the code is in the onload or after the form.

  8. #8
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    Leeds, U.K.
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the code is linked in the <head> (external .js sheet), and is only called once the user presses submit.

  9. #9
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So its in a function?

    Could you post the form?

  10. #10
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    Leeds, U.K.
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea sure, the page is:

    http://munkydesigns.co.uk/ScriptsNew/register.php

    the js for the validation is http://munkydesigns.co.uk/ScriptsNew...eckregister.js

    this should surely be something simple though? its just joining variables up.

  11. #11
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should be form[checkArray[i][0]].value instead of form.checkArray[i][0].value.

  12. #12
    SitePoint Zealot
    Join Date
    Feb 2007
    Location
    Leeds, U.K.
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nail on the head!


    cheers M M J, youve been a great help

  13. #13
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should have seen it earlier.


    Your welcome.


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
  •