2013-04-02 14 views
0

IM在我的網站:如何使用這種形式的驗證腳本添加選擇框表單驗證

$(document).ready(function(){ 
    // Place ID's of all required fields here. 
    required = ["name", "email", "country", "message",]; 
    // If using an ID other than #email or #error then replace it here 
    email = $("#email"); 
    errornotice = $("#error"); 
    // The text to show up within a field when it is incorrect 
    emptyerror = "Please enter a value in field"; 
    emailerror = "Please enter a valid e-mail."; 

    $("#theform").submit(function(){  
     //Validate required fields 
     for (i=0;i<required.length;i++) { 
      var input = $('#'+required[i]); 
      if ((input.val() == "") || (input.val() == emptyerror)) { 
       input.addClass("needsfilled"); 
       input.val(emptyerror); 
       errornotice.fadeIn(750); 
      } else { 
       input.removeClass("needsfilled"); 
      } 

     } 
     // Validate the e-mail. 
     if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) { 
      email.addClass("needsfilled"); 
      email.val(emailerror); 
     } 

     //if any inputs on the page have the class 'needsfilled' the form will not submit 
     if ($(":input").hasClass("needsfilled")) { 
      return false; 
     } else { 
      errornotice.hide(); 
      return true; 
     } 
    }); 

    // Clears any fields in the form when the user clicks on them 
    $(":input").focus(function(){  
     if ($(this).hasClass("needsfilled")) { 
      $(this).val(""); 
      $(this).removeClass("needsfilled"); 
     } 
    }); 
}); 

的問題是,我沒有代碼的選擇框

任何人都可以給我的代碼驗證選擇框? 我的選擇框中的名稱和ID是「國」 並且如果選擇停留在「國家」我希望有一個錯誤通知

回答

1

使用選定的選項屬性

var country = $('#country option:selected').val(); 

爲你的代碼循環會喜歡

for (i=0;i<required.length;i++) { 
     var input = $('#'+required[i]); 

     if(input.attr('id')== 'country' && $('#country option:selected').val() == "") { 
      input.addClass("needsfilled"); 
      input.val(emptyerror); 
      errornotice.fadeIn(750); 

     }else if ((input.val() == "") || (input.val() == emptyerror)) { 
      input.addClass("needsfilled"); 
      input.val(emptyerror); 
      errornotice.fadeIn(750); 
     } else { 
      input.removeClass("needsfilled"); 
     } 
+0

這個沒有工作unfortuantely – user2232538

+0

其實我修改了它和它的工作:VAR國家= $( '#國家選項:選擇')。VAL(); \t \t \t如果(國家== 「」) \t \t \t { (「#msg」)。html(「請選擇國家」); \t \t \t return false; \t \t \t} – user2232538

0

var cntry = $("#country").val(); 
if(empty(cntry)) 
{ 
    alert('Select a country'); 
    return false; 
} 
+0

爲你工作.. ?? – Gautam3164

+0

不,它沒有...也許是因爲如果(空應該是如果選擇國家,因爲我的下拉框中的默認選項不是空的,而是「國家」 – user2232538

0

嘗試你在尋找像http://jsfiddle.net/X5r8r/1109/

<select id="ddlViewBy"> 
<option value=""></option>  
<option value="1">test1</option> 
<option value="2">test2</option> 
<option value="3">test3</option> 
</select> 

<button id="submit"> submit </button> 

$(document).ready(function(){ 
    $("#submit").click(function(){ 
     var select_box_value = $("#ddlViewBy").val(); 
     if(select_box_value == ""){ 
      alert('select value') 
     }else{ 
     alert('success') 
     } 
    }); 
}); 
+0

是這就是我想要的但我不想彈出框 – user2232538