選擇我有我的一切形式的工作,除了要驗證從選擇菜單中的選項已經chosen..it顯示錯誤,但一旦選擇一些犯規走開檢查,以確保一個選項已從<select></select>列表
在html
<div id="budget_input">
<label for="budget">Budget</label>
<select name="mydropdown">
<option value="" id="mydropdown"> -- choose one --</option>
<option value="firstchoice">$0 - $1,000</option>
<option value="secondchoice">$1,000 - $2,000</option>
<option value="thirdchoice">$3,000 +</option>
</select>
</div>
PHP
$name = trim(stripslashes(htmlspecialchars($_POST['name'])));
$email = trim(stripslashes(htmlspecialchars($_POST['email'])));
$mydropdown = trim(stripslashes(htmlspecialchars($_POST['mydropdown'])));
$error_message = '';
$reg_exp = "/^[a-zA-Z0-9._%+-][email protected][a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";
if (!preg_match($reg_exp, $email)) {
$error_message .= "<p>A valid email address is required.</p>";
}
if (empty($name)) {
$error_message .= "<p>Please provide your name.</p>";
}
if (empty($mydropdown)) {
$error_message .= "<p>Please select an item from the list.</p>";
}
JS部分示出了用於在客戶端
var valid = '';
var required = ' is required.';
var name = $('form #name').val();
var email = $('form #email').val();
var mydropdown = $('form #mydropdown').val();
if (name == '' || name.length <= 2) {
valid = '<p>Your name' + required +'</p>';
}
if (!email.match(/^([a-z0-9._-][email protected][a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<p>Your email' + required +'</p>';
}
if (mydropdown == '') {
valid += '<p>An item from the list' + required +'</p>';
}
如果這是世界結束前解決,我會開心死:) – Blake
不適用'ID = 「mydropdown」'來'