未在提交按钮点击

时间:2016-07-14 12:15:45

标签: javascript php html

<script>
function ValidateEmail(){
    var emailID = document.getElementById("email").value;
	var email = document.getElementById("email");
	var emailRegexp=/^[a-z]+\w+([\.-]?\w+)*@[a-z]+\w+([\.-]?\w+)*(\.[a-z]{2,3})+$/i;

    if ((emailID==null)||(emailID=="")){
      //  alert("Please Enter your Email ID");
		email.style.borderColor="red";
		document.getElementById("err").innerHTML = "Please Enter your Email ID";
        emailID.focus();
        return false;
    }
	else
 if (!emailRegexp.test(emailID)){
	//alert("Please Enter valid Email ID");
	email.style.borderColor="red";
	document.getElementById("err").innerHTML = "Please Enter valid Email ID";
	emailID.focus();
return false;
	}
	else
	{
		email.style.borderColor="#e1e1e1";
		document.getElementById("err").innerHTML ="";
		return true;
	}

 }

 function validUsername()
{
	var error = "";
	var illegalChars = /\W/; // No special Characters allowed
	var fd =document.getElementById("name");

	if (fd.value == "" || fd.value == null)
	{
		fd.style.borderColor = 'Red';
		document.getElementById("UserErr").innerHTML = " Field is left Blank.\n";
		return false;
	}
	else if ((fd.value.length < 5) || (fd.value.length > 20)) // Number of Character entered is checked
	{
		fd.style.borderColor = 'Red';
		document.getElementById("UserErr").innerHTML = "Username is should be in a range of 5 and 15..\n";
		return false;
	}
	else if (illegalChars.test(fd.value)) // check for illegal characters
	{
		fd.style.borderColor = 'Red';
		document.getElementById("UserErr").innerHTML = "Illegal Characters not allowed\n";
		return false;
	} 
	else
	{
		fd.style.borderColor = '#e1e1e1';
		document.getElementById("UserErr").innerHTML = "";
		return true;
	}
}

function validPassword()
{
	var error = "";
	var password=document.getElementById("pass");
	var passError=document.getElementById("PassErr");
	var illegalChars = /[\W_]/; // Numbers and letter only
	var checkPass=/\w*[a-z]+\d+\w*/i;

	if (password.value == "" || password.value == null)
	{
		password.style.borderColor = 'Red';
		passError.innerHTML = "Field Cannot be blank.\n";
		return false;
	}
	else if ((password.value.length < 8) || (password.value.length > 20)) // Checks length of the password
	{
		password.style.borderColor = 'Red';
		passError.innerHTML = "Length should be in Range of 8 to 20. \n";
		return false;
	}
	else if (illegalChars.test(password.value))
	{
		password.style.borderColor = 'Red';
		passError.innerHTML = "Illegal characters not allowed.\n";
		return false;
	}
	else if (!checkPass.test(password.value)) // Checks for numeric value  in entered password
	{
		password.style.borderColor = 'Red';
		passError.innerHTML = "Atleast one Numeric value Required ";
		return false;
	}
	else 
	{
		password.style.borderColor = '#e1e1e1';
		passError.innerHTML = "";
		return true;
	}
}

function validateOnSubmit()
{
	if(ValidateEmail() && validUsername() && validPassword());
		return true;
		
		return false;
}
</script>
<form method="post" name="form">
<!--<input type="text" name="email" id="email" placeholder="Your Email" onblur="return ValidateEmail()"/><span id="err"></span></td>-->
<table align="center" width="30%" border="0">
<tr>
<td><input type="text" name="uname" id="name" placeholder="User Name" onblur="validUsername()"/><span id="UserErr" style="color:red"></span></td>
</tr>
<tr>
<td><input type="text" name="email" id="email" placeholder="Your Email" onblur="ValidateEmail()"/><span id="err"></span></td>
</tr>
<tr>
<td><input type="password" name="pass" id="pass" placeholder="Your Password" onblur="validPassword()" /><span id="PassErr" style="color:red"></span></td>
</tr>
<tr>
<td><button type="submit" onsubmit="return validateOnSubmit()" name="btn-signup">Sign Me Up</button></td>
</tr>
</table>
</form>
我已创建注册表单并在JavaScript中为输入字段创建验证。

onBlur验证已完成,并且运行正常。

但是,当我点击“Sign Me Up”按钮时,验证未完成,数据将插入数据库。我需要帮助。

2 个答案:

答案 0 :(得分:6)

abstract class Animal { abstract getType(): string; } class Dog extends Animal { getType(): string { return "dog"; } } class MyClass<T extends Animal> { private animal: T; constructor(animal: T) { this.animal = animal; console.log(`animal type: ${ this.animal.getType() }`) } } let dog = new Dog(); let a = new MyClass(dog); // animal type: dog 个事件触发表单,而非提交按钮。将submit属性移至onsubmit开始标记。

答案 1 :(得分:0)

&#34; emailID.focus(); &#34;错误的是在ValidateEmail()函数中。而不是它 &#34; email.focus();是正确的。 现在它按照我的预期正常工作。

但是没有必要这样我就把它删除了。