Need Help with some javascript validation.

Syran

Golden Member
Dec 4, 2000
1,493
0
76
I'm working on a form that uses javascript-coder's gen_validatorv4 script. (Have used it in the past to great success). This form is so that tellers at the bank I work at can put information in on voided checks, to send back to our accounting department and get kicked out as a csv for Foxtrot.

I have tested the script in Firefox and Chrome, with no problems. However, in IE 8 and 9, I get the message "Line #1: Your account Number must be a number!", which is a validation error for not putting a number in that line, and as far as I can figure, it's because I'm using a 1D array. It will repeat again with an error when attempting to do it under maxlen as well. I'm using the array (which is created by the previous form) as there is a variable set of number of checks which may be submitted at any given time.

I added the array-0.9.js file from js-methods, and that has slightly fixed the issue by adding an ability to handle indexes in Internet Explorer. It now properly validates the first line of items up to the radio button, but fails at that point. If I remove the radio button check, it will then fail on the 2nd row on num and maxlen verification. Please check this fiddle for the code. I've intentionally not included it in this part of the thread, as it gets kinda long.

Anyone have any ideas how to make it work in Internet Explorer properly?

Thanks!
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
I don't have a solution for you. I just wanted to say that I hope you're performing server-side validation too. You shouldn't rely on client-side validation only.
 

Syran

Golden Member
Dec 4, 2000
1,493
0
76
I do have server side validation on it, just like writing the notifications and processing in a JS better. Someone had an idea that it could be because my types are starting with numbers could cause it to not be supported by IE. I'll check it out tomorrow. :)
 

GregGreen

Golden Member
Dec 5, 2000
1,688
5
81
I do have server side validation on it, just like writing the notifications and processing in a JS better. Someone had an idea that it could be because my types are starting with numbers could cause it to not be supported by IE. I'll check it out tomorrow. :)

Sorry, but this isn't the most coherent string of paragraphs I've ever written. When coming up with debug ideas, I very much work in a stream of consciousness type thing and apparently that is true when trying to put these ideas on paper too.

Are you sure that Account Number is a number? Just pulling the number out of the input field, it will be a string so you would have to do some type coercion or use parseInt to make it a number.

I can't find it in any of my reference books right now so I might be crazy, but I'm pretty sure that starting variable names with a number is a forbidden thing.

BUT,
I'm hesitant to say that is the problem because if that were the problem, I would expect it to be thrown the first time you run the addValidation method with 1[actnum], which would yell at you with "Line #1: You must enter an account number!" instead of "Line #1: Your account Number must be a number!". Unless of course, it isn't running in the same order as the order you added the validation, then maybe I'm jumping the gun.

I'd recommend firing up IE9 and setting a breakpoint and walking your way through what's going on. If you aren't familiar with debugging in that way, read about it first because it is super useful :D. And if you are familiar and you didn't figure out the problem, sorry for insinuating you didn't know how and sorry that I don't have a solution :(.
 

Syran

Golden Member
Dec 4, 2000
1,493
0
76
Sorry, but this isn't the most coherent string of paragraphs I've ever written. When coming up with debug ideas, I very much work in a stream of consciousness type thing and apparently that is true when trying to put these ideas on paper too.

Are you sure that Account Number is a number? Just pulling the number out of the input field, it will be a string so you would have to do some type coercion or use parseInt to make it a number.

I can't find it in any of my reference books right now so I might be crazy, but I'm pretty sure that starting variable names with a number is a forbidden thing.

BUT,
I'm hesitant to say that is the problem because if that were the problem, I would expect it to be thrown the first time you run the addValidation method with 1[actnum], which would yell at you with "Line #1: You must enter an account number!" instead of "Line #1: Your account Number must be a number!". Unless of course, it isn't running in the same order as the order you added the validation, then maybe I'm jumping the gun.

I'd recommend firing up IE9 and setting a breakpoint and walking your way through what's going on. If you aren't familiar with debugging in that way, read about it first because it is super useful :D. And if you are familiar and you didn't figure out the problem, sorry for insinuating you didn't know how and sorry that I don't have a solution :(.

Yeah, I set breakpoints and worked it that way too. Wanted to use the actual code as is to get help. Seems someone over at the [H]ardForums figured it out for me. IE doesn't recognize datatypes that start with a number properly (which is really weird for arrays!).

Got it working here by changing it to a1[field] vs 1[field]. But that really didn't sit right, it makes it (imho) harder to traverse an array with that kind of name, so I ended up going with a 2D array here using stuff[1][field]. It tested and worked.
 
Last edited:

GregGreen

Golden Member
Dec 5, 2000
1,688
5
81
Yeah, I set breakpoints and worked it that way too. Wanted to use the actual code as is to get help. Seems someone over at the [H]ardForums figured it out for me. IE doesn't recognize datatypes that start with a number properly (which is really weird for arrays!).

Got it working here by changing it to a1[field] vs 1[field]. But that really didn't sit right, it makes it (imho) harder to traverse an array with that kind of name, so I ended up going with a 2D array here using stuff[1][field]. It tested and worked.

I am glad you got it working, but (and I can't say this for a certainty without seeing what your data looks like) I'm pretty sure you are making this harder than it needs to be because you aren't used to JS.

1[key] and a1[key] are exactly the same except for the name of the variable/object. It's important to remember that anything that isn't a string, number, or boolean is an object in JS. An array is just an object without a property name to iterate over. I'm only rambling on because I'm trying (and I think unsuccessfully) to convey that although it makes more sense to you right now, it sounds like you added another layer of complexity to your project.

EDIT: Thought about this a little bit this morning and wanted to clarify what I meant.

Right now, your data looks like this:
Code:
stuff = {
    "1": {
        actnum: 12345,
        serial: 12345,
        amount: 12345,
        type: "CC"
    },    
    "2": {
        actnum: 12345,
        serial: 12345,
        amount: 12345,
        type: "CC"
    }
};

It works. You could call it a day.

I think it should look like:
Code:
stuff = [
    {
        actnum: 12345,
        serial: 12345,
        amount: 12345,
        type: "CC"
    },
    {
        actnum: 12345,
        serial: 12345,
        amount: 12345,
        type: "CC"
    }
];

All the cool stuff you can do with Arrays might be worth the time to change it.

EDIT, Part 2: I just realized that you might not be assembling the stuff object yourself and you wouldn't necessarily have control over this. If that's the case, carry on.
 
Last edited:

Syran

Golden Member
Dec 4, 2000
1,493
0
76
I am glad you got it working, but (and I can't say this for a certainty without seeing what your data looks like) I'm pretty sure you are making this harder than it needs to be because you aren't used to JS.

1[key] and a1[key] are exactly the same except for the name of the variable/object. It's important to remember that anything that isn't a string, number, or boolean is an object in JS. An array is just an object without a property name to iterate over. I'm only rambling on because I'm trying (and I think unsuccessfully) to convey that although it makes more sense to you right now, it sounds like you added another layer of complexity to your project.

EDIT: Thought about this a little bit this morning and wanted to clarify what I meant.

Right now, your data looks like this:
Code:
stuff = {
    "1": {
        actnum: 12345,
        serial: 12345,
        amount: 12345,
        type: "CC"
    },    
    "2": {
        actnum: 12345,
        serial: 12345,
        amount: 12345,
        type: "CC"
    }
};

It works. You could call it a day.

I think it should look like:
Code:
stuff = [
    {
        actnum: 12345,
        serial: 12345,
        amount: 12345,
        type: "CC"
    },
    {
        actnum: 12345,
        serial: 12345,
        amount: 12345,
        type: "CC"
    }
];

All the cool stuff you can do with Arrays might be worth the time to change it.

EDIT, Part 2: I just realized that you might not be assembling the stuff object yourself and you wouldn't necessarily have control over this. If that's the case, carry on.

The problem with removing the enumeration is that it causes the form to fail on the radio buttons. The browser would see all the radio buttons as one object, and only allow one answer for the whole listing. At least so far as I could figure. The reason I did the 2D array was more for the php processing on the back end, I felt better traversing the array with a simple $_POST[stuff][++$i] then $_POST["a".++$i]. It probably honestly doesn't matter, I just felt it looked better that way.