validation forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolodede
    New Member
    • Apr 2009
    • 63

    validation forms

    i have form that contain different questions i put star for mandatory questions others left it without it then i put this java script to validate this mandatory but still ask the user to fill even the non mandatory questions and i cant submit the form why?
    Code:
    function checkreturn1(obj) 
    	{
    		var Invalid = false;
    		
    		for (i=0; i<document.getElementById("forms").length; i++)
    		{
    			if (document.getElementById("forms").checked)
    				Invalid = true;
    		}
    		if (!Invalid)
    		{
    			alert("Please Complete all Mandatory fields marked with (*)before submitting");
    			return (Invalid);
    		}
    		else
    			return (Invalid);
    		
    	}
    	
    
    //-->
    
    <form id="forms" method="post" action="http://tl28serv.uws.edu.au/twainfo/form.asp" onsubmit="return checkreturn1(this)"  >
    <h1> Advanced Standing Application </h1>
    <h3>1- Personal Details </h3>
    <h4> The questions with * are mandatory so please answer them-Thank you</h4>
    <p> Are you currently enrolled at UWS?*<br/>
    <input type="radio" name="att" value="n"/> No <br/>
    <input type="radio" name="att" value="y"/> Yes then  Student ID 
    <input type="text" name="ID" size="30" maxlength="8" onblur="checkNum(this)" />  </p>
    <p> Are you an international student?* <br/>
     <input type="radio" name="int" value="N"/> No <br/>
     <input type="radio" name="int" value="Y"/> Yes </p>
    <p> Date of Birth (dd/mm/yyy): <input type="text" name="DOB" size="30" /> </p>
    <p> Title*: 
    <select name="title" size="1">
    <option value="mr">Mr</option>
    <option value="mrs">Mrs</option>
    <option value="miss">Miss</option>
    <option value="ms">Ms</option> 
    <option value="dr">dr</option></select> &nbsp; 
     Faily name*: <input type="text" size="20" maxlength="35" name="lname" maxlength="35" onblur="validateText(this);" /> &nbsp; 
     Given names*: <input type="text" size="20" name="fname" maxlength="35" onblur="validateText(this);" /> </p>
    <p><fieldset>
    <legend> Postal address </legend>
    Unit No: <input type="text" size="5" name="no"/> &nbsp; Street No <input type="text" size="5" name="stno"/>
     &nbsp; Street name <input type="text" size="15" name="st" /> <br/><br/>
     Suburb <input type="text" size="20" name="sub" /> &nbsp;
     State <select name="state" size="1">
     <option value="NSW"> New South Wales </option>
     <option value="VIC"> Victoria</option>
     <option value="QLD"> Queensland </option>
     <option value="SA"> South Australia</option>
     <option value="ACT"> Australia Capital Territory </option>
     <option value="NT"> Nourth Territory </option>
     <option value="TAS"> Tasmania </option>
     <option value="WA"> Western Australia</option> </select> &nbsp;
     PostCode <input type="text" name="PC" size="10" maxlength="4" onblur="checkPC(this)" /> 
     </fieldset></p>
     <p> Home phone number <input type="text" name="home" size="15" /> &nbsp;
         Work phone number <input type="text" name="work" size="15" /> &nbsp;
    	 Mobile phone number <input type="text" name="mob" size="15" /> </p>
     <p> Give details of the course you are requesting that Advanced Standinding be given:<br/> <br/>
    Course number*: <input type="text" name="courseno" size="15"/> &nbsp;
    Course name: <input type="text" name="coursename" size="20"/></p> 
     <h3>2-Important information and Application checklist </h3>
    Last edited by Dormilich; Apr 11 '09, 07:26 AM. Reason: please use [code] tags!
  • lolodede
    New Member
    • Apr 2009
    • 63

    #2
    please answer me asp
    thanks

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      there are several things.

      getElementById( ) returns a single element, there is no such property as length (to be bold, length would be 1 (element found) or 0 (element not found))

      neither is the need of a loop (using getElementById( )).

      forms do not have a checked attribute (radiobuttons and checkboxes have)

      if you return true (invalid) the form is submitted (you need return false to abort)

      you can call this everywhere in the function (no need to pass it via parameter or access it with getElementById( ))

      Comment

      • lolodede
        New Member
        • Apr 2009
        • 63

        #4
        i didnt get what u said can u explain in example

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          after running your form through FireBug I see the main problem is line 7. you try to check the checked attribute of your form tag (which doesn't exist).

          use
          Code:
          document.getElementById("forms").elements[i]
          to access each of the form's elements.

          for my return statement, I have not been looking properly (sorry), although the first time the variable Invalid is set to true, it will never be changed back to false.

          and I never thought that there are elements having a length attribute (sorry too, I'm not using much forms)

          nevertheless, you're looping (well, intend to) through all form elements. the radio buttons could prove troublesome, because either of each pair is always unchecked.

          Comment

          • lolodede
            New Member
            • Apr 2009
            • 63

            #6
            thanks for explanation its working now

            Comment

            Working...