这是我的尝试。我知道它已经被回答了......但我已经完成了这项工作。
JSFIDDLE
http://jsfiddle.net/4JHy2/
HMTL
<ul class='errors' id='errors'></ul>
<form id='myForm'>
<label for'inputName'>Name:</label><br>
<input type='text' id='inputName'>
<br><br>
<label for'inputEmail'>Email:</label><br>
<input type='text' id='inputEmail'>
<br><br>
<label for'inputTel'>Telephone:</label><br>
<input type='text' id='inputTel'>
<br><br>
<input type='submit' value='Submit' id='submit'>
</form>
Javascript
var regExp = /^$|^ +$/,
elements = [],
eLength,
formValid = false,
errors;
function clearErrors() { errors.html('') }
function addError() { errors.append($('<li>Form disabled until all fields complete.</li>')) }
$(function(){
// Cache the form elements for performance and loopage.
elements.push($('#inputName'));
elements.push($('#inputEmail'));
elements.push($('#inputTel'));
eLength = elements.length;
errors = $('#errors');
// Listen for activity on all three elements.
$('#inputName, #inputEmail, #inputTel').on('keyup', function(){
// Clear previous errors.
clearErrors();
for(var i = 0; i < eLength; i++) {
if (regExp.test($(elements[i]).val())) {
formValid = false;
} else {
formValid = true;
}
}
if (!formValid) {
addError();
}
});
$('#myForm').on('submit', function(e){
e.preventDefault();
// Clear previous errors.
clearErrors();
if (formValid) {
alert("valid");
} else {
addError();
}
});
});