I have always used jQuery validate() for forms, but have run into a problem recently after an upgrade to our XMPie software (we do targeted communications).
The new version of XMPie requires jQuery 1.10.2 which is a version not supported by validate(), so I'm doing the validation manually.
I can easily validate by writing a separate function for each input, but it means re-writing at least some of the code to target a specific input name or id for example.
What I'm wondering is why can't I write a generic function for a specific input type (for example a simple text field) and let the input call the function on focusout(), passing itself as a parameter?
If I've got two text inputs, "fullName" and "userName" why can't I use
$(document).ready(function () {
var inputName = "";
var inputValue = "";
var inputAlert = "";
$("input").focusout(function () {
inputIdentify(this);
console.log(inputName);
inputGetValue(this);
console.log(inputValue);
if (this.type == "text") {
console.log("YES");
textValidate(inputName, inputValue);
}
});
function inputIdentify(theInput) {
inputName = theInput["name"];
console.log(inputName);
return inputName;
}
function inputGetValue(theInput) {
inputValue = theInput["value"];
return inputValue;
}
function textValidate(theInput, inputValue) {
console.log(theInput,inputValue);
var letters = /^[A-Za-z ]+$/;
if (inputValue.match(letters)) {
$(theInput).addClass("correct");
return true;
} else {
$(theInput).removeClass("correct");
$(theInput).addClass("incorrect");
// alert('Username must have alphabet characters only');
$(inputName).focus();
return false;
}
}
});
to remove and add simple css classes (coloured border) to indicate the problem fields?
Thanks and regards,
Malcolm