I have some input HTML elements which I want to validate whether they are filled up or not. Basically, they are mandatory. I am doing something like this:
var displayNames new Array();
displayNames[0] = "Name";
displayNames[1] = "Address";
displayNames[2] = "Age";
function validForm() {
var Name = document.getElementById(txtName).value;
if(!Name) {
alert(displayName[0] is required);
}}
How should I make the validations easy in JS?
It depends. Are they all strings? Or can some be numbers? Or some in need of a regex (like an email address)? Heck, can some even be an empty string or blank?
If we are talking about less than 12 fields, then I would consider rolling my own validation. However, if we are talking a lot of fields spread across multiple pages and projects then I would consider using the a validation plugin such as http://bassistance.de/jquery-plugins/jquery-plugin-validation/