Javascript 表单在验证之前提交
我刚刚开始用HTML、PHP和JavaScript编程。我的数据被传递到数据库中,但在提交之前并没有得到验证。 文件名='logout.html' 以下是验证代码:Javascript 表单在验证之前提交,javascript,html,forms,Javascript,Html,Forms,我刚刚开始用HTML、PHP和JavaScript编程。我的数据被传递到数据库中,但在提交之前并没有得到验证。 文件名='logout.html' 以下是验证代码: <script>function Submit1() { var emailRegex = /^[A-Za-z0-9._]*\@[A-Za-z]*\.[A-Za-z]{2,5}$/; var fname = document.form.Name.value, lname = document.
<script>function Submit1()
{
var emailRegex = /^[A-Za-z0-9._]*\@[A-Za-z]*\.[A-Za-z]{2,5}$/;
var fname = document.form.Name.value,
lname = document.form.LastName.value,
femail = document.form.Email.value,
freemail = document.form.enterEmail.value,
fpassword = document.form.Password.value,
fmonth = document.form.birthday_month.value,
fday = document.form.birthday_day.value,
fyear = document.form.birthday_year.value;
if( fname == "" )
{
document.form.Name.focus() ;
document.getElementById("errorBox").innerHTML = "enter the first name";
return false;
}
if( lname == "" )
{
document.form.LastName.focus() ;
document.getElementById("errorBox").innerHTML = "enter the last name";
return false;
}
if (femail == "" )
{
document.form.Email.focus();
document.getElementById("errorBox").innerHTML = "enter the email";
return false;
}else if(!emailRegex.test(femail)){
document.form.Email.focus();
document.getElementById("errorBox").innerHTML = "enter the valid email";
return false;
}
if (freemail == "" )
{
document.form.enterEmail.focus();
document.getElementById("errorBox").innerHTML = "Re-enter the email";
return false;
}else if(!emailRegex.test(freemail)){
document.form.enterEmail.focus();
document.getElementById("errorBox").innerHTML = "Re-enter the valid email";
return false;
}
if(freemail != femail){
document.form.enterEmail.focus();
document.getElementById("errorBox").innerHTML = "emails are not matching, re-enter again";
return false;
}
if(fpassword == "")
{
document.form.Password.focus();
document.getElementById("errorBox").innerHTML = "enter the password";
return false;
}
if (fmonth == "") {
document.form.birthday_month.focus();
document.getElementById("errorBox").innerHTML = "select the birthday month";
return false;
}
if (fday == "") {
document.form.birthday_day.focus();
document.getElementById("errorBox").innerHTML = "select the birthday day";
return false;
}
if (fyear == "") {
document.form.birthday_year.focus();
document.getElementById("errorBox").innerHTML = "select the birthday year";
return false;
}
if(document.form.radiobutton[0].checked == false && document.form.radiobutton[1].checked == false){
document.getElementById("errorBox").innerHTML = "select your gender";
return false;
}
if(fname != '' && lname != '' && femail != '' && freemail != '' && fpassword != '' && fmonth != '' && fday != '' && fyear != ''){
return true;
document.getElementById("errorBox").innerHTML = "form submitted successfully";
}
}</script>
函数Submit1()
{
var emailRegex=/^[A-Za-z0-9.\]*\@[A-Za-z]*\.[A-Za-z]{2,5}$/;
var fname=document.form.Name.value,
lname=document.form.LastName.value,
femail=document.form.Email.value,
freemail=document.form.enterEmail.value,
fpassword=document.form.Password.value,
fmonth=document.form.birth\u month.value,
fday=document.form.birthday\u day.value,
fyear=document.form.birth\u year.value;
如果(fname==“”)
{
document.form.Name.focus();
document.getElementById(“errorBox”).innerHTML=“输入名字”;
返回false;
}
如果(lname==“”)
{
document.form.LastName.focus();
document.getElementById(“errorBox”).innerHTML=“输入姓氏”;
返回false;
}
如果(femail==“”)
{
document.form.Email.focus();
document.getElementById(“errorBox”).innerHTML=“输入电子邮件”;
返回false;
}否则,如果(!emailRegex.test(femail)){
document.form.Email.focus();
document.getElementById(“errorBox”).innerHTML=“输入有效电子邮件”;
返回false;
}
如果(免费邮件==“”)
{
document.form.enterEmail.focus();
document.getElementById(“errorBox”).innerHTML=“重新输入电子邮件”;
返回false;
}如果(!emailRegex.test(免费邮件)){
document.form.enterEmail.focus();
document.getElementById(“errorBox”).innerHTML=“重新输入有效电子邮件”;
返回false;
}
如果(免费邮件!=女性邮件){
document.form.enterEmail.focus();
document.getElementById(“errorBox”).innerHTML=“电子邮件不匹配,请重新输入”;
返回false;
}
如果(fpassword==“”)
{
document.form.Password.focus();
document.getElementById(“errorBox”).innerHTML=“输入密码”;
返回false;
}
如果(fmonth==“”){
document.form.birth_month.focus();
document.getElementById(“errorBox”).innerHTML=“选择生日月份”;
返回false;
}
如果(fday==“”){
document.form.birth_day.focus();
document.getElementById(“errorBox”).innerHTML=“选择生日”;
返回false;
}
如果(fyear==“”){
document.form.birth_year.focus();
document.getElementById(“errorBox”).innerHTML=“选择生日年”;
返回false;
}
if(document.form.radiobutton[0]。选中==false&&document.form.radiobutton[1]。选中==false){
document.getElementById(“errorBox”).innerHTML=“选择您的性别”;
返回false;
}
如果(fname!=''&&lname!=''&&femail!=''&&freemail!=''&&fpassword!=''&&fmonth!=''&&fday!=''&&fyear!=''){
返回true;
document.getElementById(“errorBox”).innerHTML=“表单已成功提交”;
}
}
这是我表格的代码:
<form name="form" onsubmit="return submit1();" action="connectivity-sign-up.php" method="POST" >
<div id="errorBox"></div>
<input type="text" name="Name" value="" placeholder="First Name" class="input_name" >
<input type="text" name="LastName" value="" placeholder="Last Name" class="input_name" >
</div>
<div id="email_form">
<input type="text" name="Email" value="" placeholder="Your Email" class="input_email">
</div>
<div id="Re_email_form">
<input type="text" name="enterEmail" value="" placeholder="Re-enter Email" class="input_Re_email">
</div>
<div id="password_form">
<input type="password" name="Password" value="" placeholder="New Password" class="input_password">
</div>
<!--birthday details start-->
<div>
<h3 class="birthday_title">Birthday</h3>
</div>
<div>
<select name="birthday_month" >
<option value="" selected >Month</option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5">May</option>
</select>
<select name="birthday_day" >
<option value="" selected>Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select name="birthday_year">
<option value="" selected>Year</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select>
</div>
<!--birthday details ends-->
<div id="radio_button">
<input type="radio" name="radiobutton" value="Female">
<label >Female</label>
<input type="radio" name="radiobutton" value="Male">
<label >Male</label>
</div>
<div>
<input type="submit" name="submit" value="Submit">
</div>
</form>
生日
月
简
二月
破坏
四月
也许
白天
1.
2.
3.
4.
5.
年
2013
2012
2011
2010
2009
女性
男性
非常感谢您的帮助,因为您的函数名是
Submit1
,但在表单标签中,您使用Submit1
您知道javascript
是区分大小写的语言,因此您必须使用其中一种只需更改
onsubmit=“return Submit1();”
或将函数名更改为Submit1
,谢谢。对不起,这是个明显的错误