Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript validation form

Status
Not open for further replies.

vin23akleh

Programmer
Mar 6, 2010
6
0
0
JO
hello
i am trying to do a validation form but i ended up with one that only works for chrome, not on IE not on Firefox! don't know why!!! and another problem it looks like that the return false doesn't even work, it proceeds to the next page anyway
HTML:
function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="-1")
    {
    alert(alerttxt);
	return false;
    }
  else
    {
    return true;
    }
  }
}
/*function alertt(thisform)
{alert(thisform);}*/
function validate_form(thisform)
{
//alert('hi');
with (thisform)
  {
  if (validate_required(i_am_a,"i_am_a must be filled out!")=="-1")
	{i_am_a.focus();
	return false;}
  }
HTML:
<form method="post" action="index.php?page=step2" onsubmit="return validate_form(i_am_a.options[i_am_a.selectedIndex].value);">
		<table class="text">
		<tr>
		<td class="text">???</td>
		<td>
			<select name="i_am_a" id="i_am_a"> <!--onchange="validate_form(i_am_a.options[i_am_a.selectedIndex].value);">-->
				<option selected value="-1">?????? ???????</option>
				<option value="MSW">??? ???? ?? ?????</option>
				<option value="WSM">????? ???? ?? ???</option>
			</select>
		</td>
		</tr><tr class="submit">
			<td>
			</td>
			<td>
			<input type="submit" value="????? ???? ?????" />
			</td>
			</tr>
		</table>
	</form>
 
I've not run the code to know, but I imagine most browsers will find that "i_am_a" is undefined. Try using a proper reference to it (e.g. getElementById, through the form elements collection, etc).

Hope this helps,

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Firefox report errors, so I would start by fixing those.

Second you're function validate_required returns either true or false,

Code:
[gray] if (value==null||value=="-1")
    {
    alert(alerttxt);
   [green] return false; [/green]
    }
  else
    {
[green]    return true;[/green]
    }[/gray]

however you are trying to compare its return value against a "-1".

Code:
[gray]if (validate_required(i_am_a,"i_am_a must be filled out!")==[red]"-1"[/red])[/gray]

Since it doesn't return that value ever, your validate_form() function cannot return the "false" to the onsubmit event, so it simply ends which is equivalent to true. So the form continues to be submitted.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
this is what i managed to collect from forums and google
Code:
<form method="post" action="index.php?page=step2" onsubmit="validate_form(this); return false;">
    <table class="text">
    <tr>
    <td class="text">Select Form 1</td>
    <td>
        <select name="selectForm1" id="selectForm1">
            <option selected value="-1">Make a choice</option>
            <option value="MSW">Value 2</option>
            <option value="WSM">Value 3</option>
        </select>
    </td>
    </tr>    
    <td class="text">Select Form 2</td>
    <td>
        <select name="selectForm2" id="selectForm2">
            <option selected value="-1">Make a choice</option>
            <option value="MSW">Value 2</option>
            <option value="WSM">Value 3</option>
            <option value="XSM">Value 3</option>
        </select>
    </td>
    </tr>
    <tr class="submit">
        <td></td>
        <td><input type="submit" value="continue" /></td>
    </tr>
    </table>
</form>
Code:
function validate_form(myForm){
    var select = myForm.getElementsByTagName('select'); // returns all the select forms in a type of array
    var numSF = select.length; // number of select forms
    var selectedIndex;
    for (var i = 0; i < numSF ; i += 1){
        selectedIndex = select[i].selectedIndex;
        if (!selectedIndex){ // ! means not
          alert(select[i].name + ' must be filled out or selected! '); 
          select[i].focus();
          return false;
        }
    }
  myForm.submit();
}

but there is a problem i didn't mention
i need it to work for any thing in the form such as input type="text" and textarea and so on, and also not for each and every one should be validated, only the fields that i want them to be validated
such as username, password, DOB, and such but not a field like address,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top