SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question javascript form validate

    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 Code:
    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 Code:
    <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>

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after

    Code:
     
    function validate_required(field,alerttxt)
    {
    add

    Code:
     
    alert(field+"\n"+alerttxt);
    what is the output from the alert()?

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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,

  4. #4
    SitePoint Addict
    Join Date
    Nov 2008
    Location
    Thailand
    Posts
    303
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure how good this is

    http://www.webcheatsheet.com/javascr...validation.php

    Might be useful

    I also posted a date validation here, but it might need to be changed depending on the date format you require

    http://www.sitepoint.com/forums/showthread.php?t=709834

    RLM


Bookmarks

Posting Permissions

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