即時通訊使用JavaScript驗證來檢查每個表單域中的值之前,表單發送到數據庫,但是當我單擊提交表單仍發送甚至沒有任何值。 爲了測試它,我點擊每個字段以清除它們,然後試圖submittin按鈕表單已通過驗證但仍未提交值?
這裏是形式
<form method="post" action="send.php" id="theform" name="theform">
<input type="text" name="firstname" id="firstname" value="First Name" onFocus="this.value=''" class="yourinfo" ><br/>
<input type="text" name="lastname" id="lastname" value="Last Name" onFocus="this.value=''" class="yourinfo"><br/>
<input type="text" name="email" id="email" value="Email Address" onFocus="this.value=''" class="yourinfo"><br/>
<div id="datepicker"></div>
<input type="hidden" name="date" id="date">
<input type="image" src="images/sbmit-button.png" name="submit" height="49" width="190" id="submit" value="submit" style="margin-top:10px; margin-left:-2px;" >
</form>
繼承人的JavaScript
$(document).ready(function(){
// Place ID's of all required fields here.
required = ["firstname", "lastname", "email"];
// If using an ID other than #email or #error then replace it here
email = $("#email");
errornotice = $("#error");
// The text to show up within a field when it is incorrect
emptyerror = "Please fill out this field.";
emailerror = "Please enter a valid e-mail.";
$("#theform").submit(function(){
//Validate required fields
for (i=0;i<required.length;i++) {
var input = $('#'+required[i]);
if ((input.val() == "") || (input.val() == emptyerror)) {
input.addClass("needsfilled");
input.val(emptyerror);
errornotice.fadeIn(750);
} else {
input.removeClass("needsfilled");
}
}
// Validate the e-mail.
if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
email.addClass("needsfilled");
email.val(emailerror);
}
//if any inputs on the page have the class 'needsfilled' the form will not submit
if ($(":input").hasClass("needsfilled")) {
return false;
} else {
errornotice.hide();
return true;
}
});
// Clears any fields in the form when the user clicks on them
$(":input").focus(function(){
if ($(this).hasClass("needsfilled")) {
$(this).val("");
$(this).removeClass("needsfilled");
}
});
});
IM還鏈接到一個jQuery文件:
<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
香港專業教育學院測試了它自己的,它似乎工作,但事情似乎是在騎它,並跳過過去驗證這個頁面
================================================ ==================================
我仍然沒有工作... basicallly你認爲它可能與我也在使用的jQuery UI日期選擇器有關與形式??香港專業教育學院不包括在表單驗證,我只是想確認名字,姓氏和電子郵件充滿了
我有這個列入我的表單頁面:
<script>
$(function() {
$("#datepicker").datepicker({
altField: '#date'
});
$('#submit').click(function() {
$('#output').html($('form').serialize());
});
});
</script>
這會不會有效果它提交即使在領域沒有價值?
東西是definatley重寫驗證並提交它
你可以使用jQuery插件不要重新發明輪子:) – veritas
@Veritas - 實際上,使用這個HTML,普通的'$(「#theform」)。validate();'使用** [這個插件](http://docs.jquery.com/Plugins/validation)驗證**會在輸入電子郵件後提交表單,而其他兩個字段保持不變。 - 看到我的回答 –