javascript form validate

Discussion in 'JavaScript' started by vin_akleh, Oct 29, 2010.

  1. #1
    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
    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>
    HTML:

     
    vin_akleh, Oct 29, 2010 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The "with" statements may be the problem. I keep seeing posts that say it should not be used, and that you should use a temporary object instead.
     
    Cash Nebula, Oct 29, 2010 IP
  3. S1M

    S1M Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yup - drop the with statement and work directly with the local vars. You're already passing them as function parameters.
     
    S1M, Oct 31, 2010 IP