JavaScript problem

Results 1 to 2 of 2

Thread: JavaScript problem

  1. #1
    Join Date
    Dec 1969

    Default JavaScript problem

    Excuse me for putting an easy question up, but im cr*p at JavaScript.<BR><BR>I&#039;m trying to write a Form validation script, and keep having the same problem;<BR><BR>I asume if i do<BR><BR>for (formitem in document.personal)<BR><BR>form item will hold a reference to the item in the form, as it loops; if this were the case,<BR><BR>document.personal.formitem.value<BR>< BR>would give me the value of that form item? But it doesn&#039;t, i replaced the line with<BR><BR>document.personal.txta.value<BR><BR>an d it worked (prompting me with &#039;you have not completed abc&#039;, as expected, i&#039;m assuming formitem must be a string, instead of the object refernce i expected, and if so does anyone know how to rectify this (or another way to proce the same effect)?<BR><BR>Any help much appreciated.<BR><BR>CP<BR><BR>My function:<BR><BR> function validate(varDelimitedString)<BR> {<BR> <BR> var arrForm="";<BR> var warning="";<BR> <BR> //arrForm=varDelimitedString.split(", ");<BR> <BR> for (formitem in document.personal)<BR> {<BR> if ((formitem.indexOf("txt")==0)&#124&#124(formitem.i ndexOf("chk")==0)&#124&#124(formitem.indexOf("rad" )==0))<BR> {<BR> if (varDelimitedString.indexOf(formitem) &#062; -1)<BR> {<BR> if (document.personal.txta.value=="")<BR> {<BR> warning +="- " +formitem.substring(3) +"";<BR> }<BR> }<BR> }<BR> }<BR> alert("You have not completed

    " +warning);<BR> <BR> return false<BR> }<BR><BR>which i&#039;m calling with<BR><BR>onSubmit="return validate(&#039;txta, txtb, txtc&#039;)"<BR><BR>obviously i have form elements named &#039;txta&#039;, txtb&#039; and &#039;txtc&#039;.

  2. #2
    Join Date
    Dec 1969

    Default RE: JavaScript problem

    &#062; formitem.indexOf("txt")= ...<BR><BR>formitem will actually give you a handle to the form item OBJECT. So, to test the name you would actually do:<BR><BR><BR><BR>and, obviously all properties can be accessed directly off of that object, I.e.:<BR><BR> alert("The value of " + + " is " + formitem.value)<BR><BR>Given that you already know the names, it would be much easier to reference the fields via the .elements property of the form object. I.e.:<BR><BR><BR><BR>////////////////////////////////<BR>// PSUEDO-CODE<BR>////////////////////////////////<BR><BR>var strngFieldNames = "txtA, chkB, selC"<BR>var arrFieldNames = strngFieldNames.split(&#039;, &#039;) // we now have an array<BR><BR>// enumerate the array grabbing the form elements<BR>for(var i = 0; i &#060; arrFieldNames.length; i++)<BR>{<BR> var formitem = document.FORMNAME.elements[arrFieldNames[i]] ;<BR> // now do stuff with the formitem object<BR>}<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts