我有一个验证一些基本表单信息的页面。用户输入信息后,如果验证没有问题,则应在单击时打开一个新窗口。如果表单字段存在问题,则应提示用户修复错误并且不应打开窗口。现在我试图为此编写代码,但我遇到的问题是无论表单是否存在问题,我的窗口都会打开。仅当表单没有问题时,该窗口才应打开。
如果有人能指出我能做些什么来解决这个小问题,我将不胜感激。
代码如下:
var name = document.getElementById("name").value;
var phone = document.getElementById("phone").value;
var nat = document.getElementById("nat").value;
var address = document.getElementById("address").value;
var town = document.getElementById("town").value;
var zip = document.getElementById("zip").value;
var userName = document.getElementById("userName").value;
if (name == "" )
{
window.alert("Please Enter your Full Name");
}
checkNr= isNaN(phone)
if(checkNr == true)
{
window.alert("PHONE NUMBER: You can only enter numbers. Please try again");
}
else if (phone == "")
{
window.alert("please enter your phone number") ;
}
if (nat == "")
{
window.alert("Please enter your nationality")
}
if(town =="")
{
window.alert("Please enter your town")
}
if(zip=="")
{
window.alert("Please enter your zip code")
}
if (userName=="")
{
window.alert("Please enter your username")
}
}
</script>
HTML
<form name="subscribe" action="mailto:" >
FULLNAME: </strong><input type="text" id="name"/><br />
PHONE NR: <input type="text" id="phone" /><br />
NATIONALITY:<input type="text" id="nat" /><br />
Address:<input type="text" id="address" /><br />
Town:<input type="text" id="town" /><br />
Zip Code: <input type="text" id="zip" /><br />
Username: <input type="text" id="userName" /><br />
Date Of lesson:<input type="text" id="dOl" value="dd/mm/yy" />
<input type="button" value="submit" onclick="validate(), openWindow()" />
</form>