I’m getting “Unable to get value of the property ‘match’: object is null or undefined” when the function below works. The interesting part is that it is working perfectly on another page. Any ideas?
function valPoBox(sender, args) {
var hasPObox = /^[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\s*(\d.)*/gi;
var StreetAddress = $('.streetaddress').val();
if (StreetAddress.match(hasPObox)) {
args.IsValid = false;
sender.ErrorMessage = "Address must not contain P.O. Box";
$('.valPoBox').attr("ErrorMessage", sender.ErrorMessage);
}
else {
args.IsValid = true;
}
}
All strings have a
matchmethod, therefore what you are dealing with must not be a string. the.val()method only returns two different things: a string when an element is found, andundefinedwhen an element is not found. With this in mind,$('.streetaddress')must be returning 0 elements. Modify the selector so that it targets the correct elements to fix the issue.