Javascript Don';除非输入了所有数据,否则无法提交

Javascript Don';除非输入了所有数据,否则无法提交,javascript,html,function,registration,Javascript,Html,Function,Registration,我有一个注册页面的功能,如果用户没有输入所需的数据,它会提醒用户填写。表单仍然提交信息,但不确定我做错了什么。我希望它不提交,直到所有的数据提交和正确 function checkdetails() { if (document.form1.txtname.value == "") {alert("Please fiill in your forename."); } if (document.form1.txtsurname.value == "") {

我有一个注册页面的功能,如果用户没有输入所需的数据,它会提醒用户填写。表单仍然提交信息,但不确定我做错了什么。我希望它不提交,直到所有的数据提交和正确

function checkdetails()
{
    if (document.form1.txtname.value == "")
    {alert("Please fiill in your forename.");
    }
    if (document.form1.txtsurname.value == "")
    {alert("Please fill in your surname.");
    }
    if (document.form1.txtusername == "")
    {alert("Please create a username.");
    }
    if (document.form1.DOBDay.selectedIndex == 0)
    {alert("Please select the day you were born.");
    }
    if (document.form1.DOBMonth.selectedIndex == 0)
    { alert("Please select the month you were born");
    }
    if (document.form1.DOBYear.selectedIndex == 0)
    {alert("Please fill in the year you were born.");
    }
    if (document.form1.txtemail.value == "")
    {alert("Please enter your e-mail address.");
    }
    if (document.form1.terms.checked == false)
    {alert("Please tick that you have read the Terms and Conditions.");
    }
}

填好表格注册。
标有*的字段是必填字段
名字*
姓*
创建用户名*
生日
月
一月
二月
前进
四月
也许
六月
七月
八月
九月
十月
十一月
十二月
白天
1.
2.
3.
4.
5.
6.
7.
8.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
年
2016
2015
2014
2013
2012
2011
2010
2009
2008
2007
2006
2005
2004
2003
2002
2001
2000
1998
1998
1997
1996
1995
1994
1993
1992
1991
1990
1989
1988
1987
1986
1985
1984
1983
1982
1981
1980
1979
1978
1977
1976
1975
1974
1973
1972
1971
1970
1969
1968
1967
1966
1965
1964
1963
1962
1961
1960
1959
1958
1957
1956
1955
1954
1953
1952
1951
1950
1949
1948
1947
电子邮件*
确认电子邮件*
密码*
确认密码*
**密码长度必须至少为8个字符。
我同意这些条款和条件



我想通过文本/电子邮件接收有关库存新产品的通知。
返回false;应该阻止它执行其操作

<form name="testForm" onsubmit="return validate();"> 

<script type="text/javascript">
function validate()
{
  if(check if your conditions are not satisfying)
  { 
    alert("validation failed false");
    returnToPreviousPage();
    return false;
  }

  alert("validations passed");
  return true;
}
</script>

函数验证()
{
如果(检查您的条件是否不满足)
{ 
警报(“验证失败错误”);
returnToPreviousPage();
返回false;
}
警报(“通过验证”);
返回true;
}
然而,正如上面的回答所述,html5的required属性是最佳实践。不过,您需要考虑的不仅仅是空字段。例如,将需要正则表达式检查您的电子邮件字段等


返回false;应该阻止它执行其操作

<form name="testForm" onsubmit="return validate();"> 

<script type="text/javascript">
function validate()
{
  if(check if your conditions are not satisfying)
  { 
    alert("validation failed false");
    returnToPreviousPage();
    return false;
  }

  alert("validations passed");
  return true;
}
</script>

函数验证()
{
如果(检查您的条件是否不满足)
{ 
警报(“验证失败错误”);
returnToPreviousPage();
返回false;
}
警报(“通过验证”);
返回true;
}
然而,正如上面的回答所述,html5的required属性是最佳实践。不过,您需要考虑的不仅仅是空字段。例如,将需要正则表达式检查您的电子邮件字段等


最好使用html5属性required

<input type="text" name="input" required >

最好使用html5属性required

<input type="text" name="input" required >


如果出现错误(函数验证(){retur false;}$(“#yourform”)。提交(函数(事件){if(!validate())event.preventDefault();});)如果出现错误(函数验证(){retur false;}$(“#yourform”)。提交(函数(事件){if(!validate())event.preventDefault();})使用此选项停止提交(函数验证(){retur false;}$(“#yourform并非所有的浏览器都支持必选的属性。在大学里从来没有听说过这个属性,我只是个新手。它创造了奇迹,万分感谢!不是所有的浏览器都支持
必需的
属性。我在大学里从没听说过这个属性,我只是个新手。它创造了奇迹,万分感谢!