HTML / JavaScript验证问题

时间:2017-09-21 18:29:11

标签: javascript html

我正在构建的网站的一部分我正在尝试验证注册。代码工作它只是一旦它验证它似乎退出的第一个名称并返回true ..我迷失了如何修复它,因为我之前没有遇到过这个问题,我看不出我的代码有什么问题。

function validateForm(){

				var x = document.registration.email.value;
				var atPos = x.indexOf("@");
				var dotPos = x.lastIndexOf(".");
	
				if(document.registration.user.value.length < 4){
					window.alert("Please enter a valid username. (4 - 20 Characters)");
					document.registration.user.focus();
					return false;
				}	
	
				if(document.registration.pass.value.length < 4){
					window.alert("Please provide a valid password. (4 - 50 Characters)");
					document.registration.pass.focus();
					return false;
				}
				
				if(atPos < 1 || dotPos < atPos + 2 || dotPos + 2 >= x.length) {
					window.alert("Invalid email format.");
					document.registration.email.focus();
					return false;
				}
				
				if(document.registration.firstname.value.length < 3){
					window.alert("Please provide a valid firstname. (3 - 20 Characters)");
					document.registration.fistname.focus();
					return false;
				}
				
				if(document.registration.surname.value.length < 3){
					window.alert("Please a valid surname. (3 - 20 Characters)");
					document.registration.surname.focus();
					return false;
				}
				
				if(document.registration.country.value.length < 3){
					window.alert("Please provide valid Country. (3 - 20 Characters)");
					document.registration.country.focus();
					return false;
				}else{
					return true;
				}
			}
<form name="registration" action="registration.php" method="post" onsubmit="return validateForm()">

2 个答案:

答案 0 :(得分:0)

只有满足您的国家条件时才会返回true。尝试使用if else if和else

function validateForm(e){
                e.preventDefault();
                var x = document.registration.email.value;
                var atPos = x.indexOf("@");
                var dotPos = x.lastIndexOf(".");

                if(document.registration.user.value.length < 4){
                    window.alert("Please enter a valid username. (4 - 20 Characters)");
                    document.registration.user.focus();
                    return false;
                }   

                else if(document.registration.pass.value.length < 4){
                    window.alert("Please provide a valid password. (4 - 50 Characters)");
                    document.registration.pass.focus();
                    return false;
                }

                else if(atPos < 1 || dotPos < atPos + 2 || dotPos + 2 >= x.length) {
                    window.alert("Invalid email format.");
                    document.registration.email.focus();
                    return false;
                }

                else if(document.registration.firstname.value.length < 3){
                    window.alert("Please provide a valid firstname. (3 - 20 Characters)");
                    document.registration.fistname.focus();
                    return false;
                }

                else if(document.registration.surname.value.length < 3){
                    window.alert("Please a valid surname. (3 - 20 Characters)");
                    document.registration.surname.focus();
                    return false;
                }

                else if(document.registration.country.value.length < 3){
                    window.alert("Please provide valid Country. (3 - 20 Characters)");
                    document.registration.country.focus();
                    return false;
                }else{
                    return true;
                }
            }

答案 1 :(得分:0)

您的名字字段输入错误:

document.registration.fistname.focus();

提交表单后,您可以检查浏览器的元素检查器是否存在任何javascript错误。