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