here’s my function for checking zipcode. When a null values comes in, i keep getting “Object Required” Does anyone know where im going wrong?
aspx tags –
asp:CustomValidator
ID="cv_zipcode"
runat="server"
ControlToValidate="tb_zipcode"
ClientValidationFunction="ValidateZipcode"
ValidateEmptyText="true"
Display="Dynamic"
ValidationGroup="vgroup">
</asp:CustomValidator>
function ValidateZipcode(sender, args) {
var regZipcode = '\d{5}'
var zipcode = document.getElementById(sender.id.replace(/cv_/, "tb_"));
if ((zipcode.value == "ZipCode") || (zipcode.value.length == null) || (zipcode.value.length == "")) {
zipcode.style.backgroundColor = "#f6e086";
args.IsValid = false; return;
} else {
args.IsValid = true;
zipcode.style.backgroundColor = "white";
}
}
I’m not sure exactly which value is null, but in general, if you have a variable x which may or may not be null, and you want to do something with x, you can do the following:
If
x == null, then this returns false and doesn’t try to executedo_something_with(). Otherwise, this expression returns the value ofdo_something_with(x).If you just
do_something_with(x), and x is null, anddo_something_with()is not expecting a null, you can get errors.EDIT:
try: