Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. Form validation - form.length

Form validation - form.length

Scheduled Pinned Locked Moved Web Development
helpjavascripthtml
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    braveheartkenya
    wrote on last edited by
    #1

    Greetings all! I'm quite new to javascript so i may have some rather silly kind of questions... but anyway here's the problem... I want to loop through all the elements in a form so as to check their values in order to validate the relevant fields, i thought i could get the number of form objects within the form using "formName.length." Then use a loop to go through all the form fields. It seems formName.length is not working for me ... it's returning "undefined" or "0" values. How could i get the number of form objects within a certain form. My code is as follows: [CODE] window.moveTo(100,100); window.resizeTo(300,460); //window.locationbar.visible=false; //window.menubar.visible=false; //window.personalbar.visible=false; //window.scrollbars.visible=false; //window.statusbar.visible=false; //window.toolbar.visible=false; //window.resize=false; //window.resizeable=no; var elementNo; elementNo = document.registrationForm.length; //elementNo = document.registrationForm.txtFirstName.value; var num; num = document.forms[0].length; var counter = 0; var myMessage = "Please ensure that you have filled in the following fields:\n"; var myFieldsAlert = ""; var myAlertFlag="0"; function validate(){ // alert("abc "+elementNo); alert("abc "+num); /**/ while(counter<elementNo){ // if(registrationForm.elements[counter].value==""){ myFieldsAlert += "-"+registrationForm.elements[counter].id+"\n"; myAlertFlag="1"; } if(registrationForm.optGenderM.value!="0" || registrationForm.optGenderF.value!="1"){ myFieldsAlert += "-Gender\n"; myAlertFlag="1"; } counter++; } /**/ /**/ if(myAlertFlag=="1"){ alert(myMessaage+myFieldsAlert); } else{ registrationForm.submit(); } myAlertFlag ="0"; /**/ return false; } [/CODE] Could someone please enlighten me on the issue at hand and propose a solution. (By the way the code is in a HTML page which is called up as a popup). Any help would be greatly appreciated...

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups