2012-01-09 19 views
0

私は名前という名前のテキストエリアを持つフォームを持っていますが、すべてのドロップダウンリストがナビゲーションであってもこのフィールドの妥当性を確認できないようですが、私は何かを逃したのですか?テキストフィールドの妥当性確認

<script type="text/javascript"> 
function validate_form() { 


    if (document.form.title.selectedIndex == 0) 
    { 
     alert ("Please Select Title."); 
     return false; 
    } 
    if (document.form.time.selectedIndex == 0) 
    { 
     alert ("Please Select Time."); 
     return false; 
    } 
    if (document.form.membership.selectedIndex == 0) 
    { 
     alert ("Please Select Membership."); 
     return false; 
    } 
    if (document.form.name.length < 1) 
    { 
     alert ("Please Enter Name"); 
     return false; 
    } 
} 

</script> 
+0

http://jsfiddle.net/4hBuF/上では、検証モジュールを提供してjQueryのを試してみましたか? –

答えて

1

が代わりに使用document.form.name.value.lengthdocument.forms[0].title.selectedIndex

+0

優れたdocument.forms [0] .title.selectedIndex作品、ありがとうございます。 –

+0

よりも受け入れ済みとマークするのを忘れないでください;) – Headshota

0

を試してみてください。

+0

名前は悪いIDです。予約語、確かに。 –

0

通常、テキストエリアまたは入力のいずれかの値の長さであるかどうかを確認するだけで十分です。

if(field.value.length) { 

} 

ベローズコード。必要な場合があります。このため

<form id="myform" method='post' onsubmit="return validate(this)"> 
    <select name="select1"> 
     <option value="">--</option> 
     <option value="val11"> Option </option> 
     <option value="val12"> Option </option> 
    </select> 

    <select name="select2"> 
     <option value="">--</option> 
     <option value="val21"> Option </option> 
     <option value="val22"> Option </option> 
    </select> 
    <select name="select3" > 
     <option value="">--</option> 
     <option value="val31"> Option </option> 
     <option value="val32"> Option </option> 

    </select> 

    <textarea name='description'> </textarea> 
    <input type="submit"/> 

</form> 

とスクリプト:

var validate = function(form) { 
    // argument is a form from which function was called 

    // Collect elements to an array which will be validated. 
    var inputs = [].concat.apply(
     [].concat.apply([], form.getElementsByTagName("select")), 
     form.getElementsByTagName("textarea")         
    ); 


    for(var i = 0, l = inputs.length, input; input = inputs[i], i < l; i++) { 

     if(!input.value ) { 
      //In case when some select don't have a value, or empty string is provided  alert(input.name); 
      // Message will be showed 
      alert("Field " + input.name + " cannot be empty!"); 
      return false; // return false, form will not submitted 
     } 
    }; 
    return true; // if all of selects have a properly value; 
}; 

デモ