这是我的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>
答案 0 :(得分:1)
没有函数document.getElementByName
,它是document.getElementsByName
复数。
更改为
var nationality = document.getElementsByName("nationality")[0].value;
如果你看一下控制台,你会看到一个错误,表明这一点。
答案 1 :(得分: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;
}
答案 2 :(得分: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/