2014-03-28 29 views
1

嘿傢伙我想通過javascript進行驗證,我試圖在互聯網上的每一種方法,它不工作,它清除每個領域和刷新頁面。 我需要的是驗證他選擇他的國籍。<select>驗證不起作用

這裏是我的javascript代碼

function signin(){ 
       var a=document.forms["signin"]["inemail"].value; 
       if(a==null || a==""){ 
       alert("Please enter an Email!!"); 
       document.getElementById("upemail").innerHTML="<font color=red>email is empty</font>" 
       return false; 
     } 
      } 
     function signUp(){ 
     var x1 = document.forms["signup"]["upemail"].value; 
     var x2 = document.forms["signup"]["uppassword"].value; 
     var x3 = document.forms["signup"]["repassword"].value; 
     var x4 = document.forms["signup"]["fname"].value; 
     var x5 = document.forms["signup"]["lname"].value; 
     var x6 = document.forms["signup"]["dob"].value; 
     var x7 = document.forms["signup"]["gender"].value; 
     var x8 = document.forms["signup"]["nationality"].value; 
     var atpos=x1.indexOf("@"); 
     var dotpos=x1.lastIndexOf("."); 
     var date = document.getElementById('dob').value; 
     var minLength = 6; 
     var nationality = document.getElementByName("nationality")[0].value; 


     if(x1==null || x1==""){ 
     alert("Please enter an Email!!"); 
     document.getElementById("upemail").innerHTML="<font color=red>email is empty</font>" 
     return false; 
     } 
     else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) 
     { 
     alert("Not a valid e-mail address"); 
     return false; 
     } 
     else if(x2==null || x2==""){ 
     alert("Please enter a password!!"); 
     return false; 
     } 
     else if(x3==null || x3==""){ 
     alert("Please re-enter your password"); 
     return false; 
     } 
     else if(document.signup.uppassword.value.length < minLength){ 
     alert("Password must be minimum 6 character"); 
     } 
     else if(x3 != x2){ 
     alert("password does not match"); 
     } 
     else if(x4==null || x4==""){ 
     alert("Please enter your first name"); 
     return false; 
     } 
     else if(x5==null || x5==""){ 
     alert("Please enter your last name"); 
     return false; 
     } 
     else if(x6==null || x6==""){ 
     alert("Please enter your Date of Birth"); 
     return false; 
     } 
     else if (form.gender[0].checked == false && form.gender[1].checked == false) { 
     alert ("Please choose your Gender: Male or Female"); return false; 
     } 
     else if (date == "" ||date == null){ 
     alert("Date of Birth must be chosen"); 
     return false; 
     } 
     else if(nationality == "none"){ 
     alert("Please enter your nationality") 
     } 
     else{ 
     alert("sucssefully") 
     return true; 
     } 


    } 

這裏是最後一次嘗試進行驗證

var nationality = document.getElementByName("nationality")[0].value; 
else if(nationality == "none"){ 
    alert("Please enter your nationality") 
    } 

,這是形式

<form name ="signup" action="" onsubmit="return signUp()" method="post"> 

的頭和我選擇的這部分標籤

<select name="nationality" class="input" id="nationality"> 
          <option value="none" selected>Country...</option> 
          <option value="Afganistan">Afghanistan</option> 
          <option value="Albania">Albania</option> 
          <option value="Algeria">Algeria</option> 
          <option value="American Samoa">American Samoa</option> 
+1

小提琴將是巨大的 –

+0

看看JavaScript控制檯 - '類型錯誤:對象#已經沒有方法「getElementByName'' – andyb

+0

什麼樣的錯誤是顯示在JavaScript安慰 – suhailvs

回答

1

沒有功能document.getElementByName,這是document.getElementsByName複數。

更改爲

var nationality = document.getElementsByName("nationality")[0].value; 

如果你看一下控制檯,你會看到一個錯誤,指出這一點。

0

請嘗試

if(document.getElementById('nationality').selectedIndex == 0) 
{ 
alert("Please select Nationality"); 
return false; 
} 

或者

objDDl = document.getElementById('nationality'); 
if(objDDl.options[objDDl.selectedIndex].value == "none") 
{ 
alert("Please select nationality"); 
return false; 
} 
0

您可以使用selectedIndex屬性,該屬性檢查選擇框的選定索引。

var elem = document.getElementById("nationality"); 
if(elem.selectedIndex == 0) 
{ 
    alert("You must choose"); 
} 
else 
{ 
    alert("You have chosen wisely"); 
} 

而他們使用return false就像你對其他領域的做法一樣。

請參見本搗鼓全碼 - http://jsfiddle.net/qb6Hb/