验证表单在注册期间不起作用
Validation form is not working during signup
我创建了一个html SignUp页面,并在javascript中进行验证。这是验证码。
注册.html
<script type="text/javascript">
function validateForm() {
var id = document.signup.LoginId;
var password = document.signup.LoginPassword;
var cpassword = document.signup.ConfirmPassword;
var city = document.signup.City;
var state = document.signup.State;
var number = document.signup.PhoneNumber;
var email = document.signup.Email;
var address = document.signup.Address;
var zipcode = document.signup.ZipCode;
if(id.value == "") {
window.alert("Error: Username should be filled out.");
id.focus();
return false;
}
re = /^'w+$/;
if(!re.test(id.value)) {
window.alert ("Error: Username must contain only letters, numbers and underscores.");
id.focus();
return false;
}
if(id.length < 6) {
window.alert("Error: Username must contain at least 6 charecters.");
id.focus();
return false;
}
if(id.length > 12) {
window.alert("Error: Username must not be greater than 12 charecters.");
id.focus();
return false;
}
if(password.value != "" && password.value == cpasswotd.value) {
if(password.length < 6) {
window.alert("Error: Password must contain at least 6 charecters.");
password.focus();
return false;
}
if(password.length > 12) {
window.alert("Error: Password must not be greater than 12 charecters.");
password.focus();
return false;
}
if(password.value == id.value) {
window.alert("Error: Password must be different from UserName.");
password.focus();
return false;
}
re = /[0-9]/;
if(!re.test(password.value)) {
window.alert("Error: Password must contain at least one number.");
password.focus();
return false;
}
re = /[a-z]/;
if(!re.test(password.value)) {
window.alert("Error: Password must contain at least one lowercase letter (a-z).");
password.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(password.value)) {
window.alert("Error: Password must contain at least one uppercase letters (A-Z).");
password.focus();
return false;
}
}else {
window.alert("Error: Please check that you've entered and Confirmed your Password.");
password.focus();
return false;
}
window.alert("You have entered a valid password: "+password.value);
return true;
if(city.value == "") {
window.alert("City must not be null.");
city.focus();
return false;
}
if(state.value == "") {
window.alert("State must not be null.");
state.focus();
return false;
}
if(number.value == "") {
window.alert("Phone number must not be null.");
number.focus();
return false;
}
if(number.length != 10) {
window.alert("Phone number must be 10 digits.");
number.focus();
return false;
}
if (email.value == "")
{
window.alert("Please enter a valid e-mail address.");
email.focus();
return false;
}
if (email.value.indexOf("@", 0) < 0)
{
window.alert("Please enter a valid e-mail address.");
email.focus();
return false;
}
if (email.value.indexOf(".", 0) < 0)
{
window.alert("Please enter a valid e-mail address.");
email.focus();
return false;
}
re = /^'w+$/
if(!re.test(address.value)) {
window.alert("Error: Address must contain only letters, numbers and underscores.");
address.focus();
return false;
}
if(zipcode.value == "") {
window.alert("Error: Zipcode must not be null.");
zipcode.focus();
return false;
}
if(zipcode.length > 6) {
window.alert("Error: zipcode must not less than 6 digits.");
zipcode.focus();
return false;
}
}脚本>
当我尝试输入错误的电子邮件地址(如"sapan@2"或"sapan")时,它不会显示任何错误消息,而是保存到数据库中。
任何人都可以帮我解决这个问题。
谢谢。
我创建了一个简单的测试注册表单,并使用jQuery附加了验证处理程序。您可以根据需要修改此测试处理程序。
将以下代码放入注册.html:
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<form id="myForm" name="myForm" action="" method="post">
Username: <input type="text" id="username" value="" />
<input type="submit" name="submit" />
</form>
<script type="text/javascript">
$( "#myForm" ).submit(function( event ) {
if ( $( "input#username" ).val() != "" ) {
alert('Logging you in!');
return;
}
alert( "Please enter the username!" );
event.preventDefault();
});
</script>
</body>
</html>
为此,您需要将 jquery.min.js 放在与 html 文件相同的文件夹中。
有几件事可能是错误的。
首先,如果您从"type="submit"的"input"控件调用"validateForm"函数,请将其更改为"type="button"。
其次,在"验证表单"函数的末尾,添加以下行...
document.getElementById("yourformidhere").submit();
这将在单击按钮时阻止表单提交,并且仅在所有元素值通过验证后提交表单,这是您实际想要完成的。
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 监视函数从服务返回不起作用,但作用域函数起作用
- 幻灯片滚动javascript不起作用
- 简单的javascript在Shopify中不起作用
- Recaptcha在IE7和IE8中不起作用
- Ember Data DS.Model's set函数不起作用
- 模态上的输入/注册字段不起作用
- 我试图使用引导模式类来制作一个弹出注册表单,但弹出没有'不起作用
- 使用PHP/MYSQL在phonegap中注册;不起作用
- 谷歌在IE浏览器中注册不起作用
- 验证表单在注册期间不起作用
- parse.com用户注册不起作用的问题
- 使用解析注册将不起作用.未知错误
- 登录&注册相同的脚本,但其中一个没有'不起作用
- 注册页面的Javascript验证不起作用
- Javascript验证在html注册表单中不起作用
- <fb:注册onvalidate=“"/>不起作用
- PhoneGap PushPlugin注册ID在moil中不起作用
- Google Adwords - 注册多个转换在IE中不起作用
- 尝试在 getElementId 不起作用的表单中注册后显示弹出窗口