
Originally Posted by
Humper
Code:
function validateForm() {
if( document.getElementById('FirstName').value == "" ||
If you are validating a form, don't use the getElementById method to access form controls. Instead, use the elements collection of the form object. For example,
Code:
function validate(form) {
/* form.elements.FirstName.value
* form.elements.LastName.value
* etc.
*
* OR:
*
* var controls = form.elements;
* controls.FirstName.value
* controls.LastName.value
* etc.
*/
}
HTML Code:
<form action="..." method="post" onsubmit="return validate(this);">
<!-- ... -->
<input type="submit" value="Submit trouble ticket">
<!-- ... -->
</form>
If validation fails, return false from the validate function, otherwise return true. Don't use the submit method. Also, note that it's not usually necessary to identify a form (using a name or id attribute) using this approach. It's also not necessary to add name attributes to submit buttons unless you actually plan to inspect the name/value pair that it creates (such as you might if there's more than one submit button, each of which does different things).
You might want to check for controls that are either empty or contain only whitespace, rather than just the former.
document.getElementById('mac').value == "" ||)
This is a syntax error: there's no expression on the right-hand side of that logical OR operator.
else if(document.getElementById('email').value.indexOf("@") == -1 || document.getElementById('email').value.indexOf(".") == -1)
Regular expressions offer more power when validating against patterns. It's also a good idea to copy values or object references to variables, rather than looking up the same thing again and again.
else if(document.getElementById('email').value.indexOf(" ") > 0)
There isn't much point in trying to over-constrain e-mail addresses on the client. If you want to validate them thoroughly, do it on the server against the complete RFC 2822 grammar.
if(document.getElementByID('Phone').value.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
If you want to check that a string matches a pattern, use the test method of the regular expression object. You also have another error here: the "D" in getElementByID must be lowercase.
Because you haven't cancelled the submission where validation fails, it will happen whether the submit method is called or not. In any case, the submit method should hardly ever be used.
Consider this implementation for validation:
Code:
function validate(form) {
var controls = form.elements,
email, phoneNumber;
if (isEmpty(controls.FirstName.value) || isEmpty(controls.LastName.value)
|| isEmpty(controls.Site.value) || isEmpty(controls.Building.value)
|| isEmpty(controls.StudentRoom.value) || isEmpty(phoneNumber = controls.Phone.value)
|| isEmpty(email = controls.email.value) || isEmpty(controls.ipaddress.value)
|| isEmpty(controls.mac.value)) {
alert('Please complete all fields.');
return false;
}
if (!isEmailAddress(email)) {
alert('Please enter a valid e-mail address.');
return false;
}
if (!isPhoneNumber(phoneNumber)) {
alert('Please enter a phone number with the format xxx-xxx-xxxx.');
return false;
}
return true;
}
function isEmailAddress(string) {
return /^[^@]+@[^.]+(\.[^.]+)+$/.test(string);
}
function isEmpty(string) {
return !/\S/.test(string);
}
function isPhoneNumber(string) {
return /^\d{3}-\d{3}-\d{4}$/.test(string);
}
using the markup approach I showed earlier.
Hope that helps,
Mike
Bookmarks