我有一个包含五个元素的表单,firstName
, lastName
, username
,password
和passwordConfirmation
var errors = [];
var errorsText = [];
function generateError(messageText, elementIdentifier) {
var messageText = messageText;
var elementIdentifier = elementIdentifier;
errors.push(elementIdentifier);
errorsText.push(messageText);
return false;
}
function validateRegistrationForm(firstName, lastName, username, password, passwordConfirmation) {
formReset();
//alert("validateRegistrationForm() has been called");
var firstName = firstName;
//alert("First Name = \'" + firstName + "\'");
var lastName = lastName;
//alert("Last Name = \'" + lastName + "\'");
var username = username;
//alert("Username = \'" + username + "\'");
var password = password;
//alert("Password = \'" + password + "\'");
var passwordConfirmation = passwordConfirmation;
//alert("Password Confirmation = \'" + passwordConfirmation + "\'");
if ( ( firstName == null ) || ( firstName == "" ) ) {
//alert("//firstname hasn't been filled out");
generateError("You must provide a First Name", "firstName");
}
if ( ( lastName == null ) || ( lastName == "" ) ) {
//lastname hasn't been filled out
generateError("You must provide a Last Name", "lastName");
}
if ( ( username == null ) || ( username == "" ) ) {
//username hasn't been filled out
generateError("You must enter a Username", "username");
}
if ( ( password == null ) || ( password == "" ) ) {
//password hasn't been filled out
generateError("You haven\'t entered a password", "password");
}
if ( ( passwordConfirmation == null ) || ( passwordConfirmation == "" ) ) {
//passwordconfirmation hasn't been filled out
generateError("You haven\'t confirmed your password", "passwordConfirmation");
}
validatePassword(password, passwordConfirmation);
}
有没有更好的方法来处理所有的if statements
?