• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javascript object how to retrieve properties

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
honestyly I have no idea why you are doing this when a simple JSON notation would do it



Eric
 
Sri Anand
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic