HTML5提供输入验证,您可以设置为了告诉浏览器您的html视图是HTML5。
//Set your doctype for HTML5.
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<form id="the_form">
//here html5 will not submit if the box is empty or does not meet the email
//addres format.
<input type="email" name="email" id="email" placeholder="Enter email..">
<input type="submit" value="send">
</form>
</body>
</html>
如果你不想使用HTML5,你也可以做,如果输入的是空一个简单的JavaScript代码无法提交。
<html>
<head>
<title></title>
<script type="text/javascript">
window.onload = init();
function init(){
//get form.
var form = document.getElementById("the_form");
form.onsubmit = email_validation;
}
function email_validation(){
email = document.getElementById("email");
if(email.value == ''){
//return false to avoid submission.
return false;
}
else{
//do whatever code.
}
}
</script>
</head>
<body>
<form id="the_form">
<input type="text" name="email" id="email" placeholder="Enter email..">
<input type="submit" value="send">
</form>
</body>
</html>
用这种方式,您的电子邮件将在发送之前进行验证,希望为您工作。
“我无法得到它的工作。”继续... – Bucket
永远不要依赖于客户端验证。无论您是否能够正常工作,您仍然需要在服务器上进行验证。 –
你忘了在小提琴中选择jquery库。 http://jsfiddle.net/4TuVU/4/ – karthikr