This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes javascript object how to retrieve properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "javascript object how to retrieve properties" Watch "javascript object how to retrieve properties" New topic
Author

javascript object how to retrieve properties

Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
function requiredif () {
this.aa = new Array("applicantId", "Service Company Number is required.", new Function ("varName","this.fieldValue[0]='Fax'; this.field[0]='appSource'; return this[varName];"));

this.ab = new Array("logNumber", "Log Number is required.", new Function ("varName","this.fieldValue[0]='Fax'; this.field[0]='appSource'; return this[varName];"));
}

I am trying to retrieved the properties fieldValue[0],field[0] i tried these and it didnt work

orequiredif = new requiredif();
for (x in orequiredif) {
// var index= "fieldValue["+"0"+"]"
// var field = form[orequiredif[x][0]];
var obj = orequiredif[x][2];
alert(orequiredif[x][2].toString());
// alert(orequiredif[x][2]("fieldValue[0]"));
// alert(orequiredif[x][2].fieldValue[0]);



}

any help
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I'm sort of curios as to what error you got when you tried to run that? "It doesn't work" doesn't give us much information to be able to help. I am sort of curious about this line:
for (x in orequiredif) I've never seen a for statement like that. what is it supposed to do? I've been scouring my O'Rielly Javascript 5th Ed, and I'm not seeing anything like that either.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Yyou really should not be using an array, I would use an object with JSON notation.

You have to reference the names of the arrays:



Eric
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
I am trying to implement client side validation for requiredif Struts validation, struts is generating java scrit objects in the format i have specified and what i meant saying it doest work was when i tried to access those properties i mentioned i get error no property found
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
It is because you do not have a multi-demensional array like you are accessing it. You have an object with two properties that contain arrays. Take a look at what I wrote.

Eric
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"></meta>
<title>untitled3</title>
<script language="JavaScript1.2">
function requiredif () {
this.aa = new Array("applicantId", "Service Company Number is required.", new Function ("varName","this.fieldValue[0]='Fax'; this.field[0]='appSource'; return this[varName];"));
this.ab = new Array("logNumber", "Log Number is required.", new Function ("varName","this.fieldValue[0]='Fax'; this.field[0]='appSource'; return this[varName];"));
}



function validateRequiredIf(form) {
var fields = new Array();
orequiredif = new requiredif();
for (x in orequiredif) {
// var index= "fieldValue["+"0"+"]"
// var field = form[orequiredif[x][0]];
var obj = orequiredif[x][2];
alert(orequiredif[x][2]);
// alert(orequiredif[x][2]("fieldValue[0]"));
// alert(orequiredif[x][2].fieldValue[0]);



}
}
</script>
<SCRIPT language="JavaScript1.2" src="json.js"/></script>

</head>
<body on load="validateRequiredIf()">
</body>
</html>


i also tried the way you suggested no luck i have also inserted a space between on and load else i was not able to post the code
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"></meta>
<title>untitled3</title>
<script language="JavaScript1.2">
function requiredif () {
this.aa = new Array("applicantId", "Service Company Number is required.", new Function ("varName","this.fieldValue[0]='Fax'; this.field[0]='appSource'; return this[varName];"));
this.ab = new Array("logNumber", "Log Number is required.", new Function ("varName","this.fieldValue[0]='Fax'; this.field[0]='appSource'; return this[varName];"));
}



function validateRequiredIf(form) {
var fields = new Array();
orequiredif = new requiredif();
for (x in orequiredif) {
// var index= "fieldValue["+"0"+"]"
// var field = form[orequiredif[x][0]];
var obj = orequiredif[x][2];
alert(orequiredif[x][2]);
// alert(orequiredif[x][2]("fieldValue[0]"));
// alert(orequiredif[x][2].fieldValue[0]);



}
}


</head>
<body on load="validateRequiredIf()">
</body>
</html>


i also tried the way you suggested no luck i have also inserted a space between on and load else i was not able to post the code
this is corrected one
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I can do a loop with no problem:





The problem is that the function is not valid



If you run it you would get the error message: this.fieldValue has no properties

Eric
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
I saw that we get default JS functions which are accessing the objects created in this format and associated JS function actually works .. well the difference is in earlier function i had arraysas field properties

here oMaxLength[x][2]("maxlength") works

function maxlength () {
this.aa = new Array("permitNumber", "Permit Number can not be greater than 12 characters.", new Function ("varName", "this.maxlength='12'; return this[varName];"));
}


function validateMaxLength(form) {
var isValid = true;
var focusField = null;
var i = 0;
var fields = new Array();
oMaxLength = new maxlength();
for (x in oMaxLength) {
var field = form[oMaxLength[x][0]];

if (field.type == 'text' ||
field.type == 'textarea') {

var iMax = parseInt(oMaxLength[x][2]("maxlength"));
if (field.value.length > iMax) {
if (i == 0) {
focusField = field;
}
fields[i++] = oMaxLength[x][1];
isValid = false;
}
}
}
if (fields.length > 0) {
focusField.focus();
alert(fields.join('\n'));
}
return isValid;
}]]>

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
honestyly I have no idea why you are doing this when a simple JSON notation would do it



Eric
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
Thank you so much for replying . I am working with Struts Framework(J2EE) which actually has some client side validation automated. I have to write scripts for the generated javascript objects which i have put in earlier post.
All this would help me have a single JS function to validate all my forms which are like 20 to 30 in number and avoid writing custom JS functions for each page
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
i Guess if the function thats generating is wrong or improper only thing i should do further is to split them up like strings and retrieve values ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javascript object how to retrieve properties