1

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";
        }
    }
2
  • You get errors when what is null? zipcode? Commented Nov 4, 2011 at 2:40
  • Can you get your element by its typed id (tb_zipcode), rather than replacing text on the sender id? Commented Nov 4, 2011 at 3:05

4 Answers 4

1

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:

x != null && do_something_with(x)

If x == null, then this returns false and doesn't try to execute do_something_with(). Otherwise, this expression returns the value of do_something_with(x).

If you just do_something_with(x), and x is null, and do_something_with() is not expecting a null, you can get errors.

EDIT:

try:

if ((zipcode == null) || (zipcode.value == null) || [everything else])
Sign up to request clarification or add additional context in comments.

Comments

0

zipcode.value.length returns an integer

I think you should have

if ((zipcode.value == "ZipCode") || (zipcode.value.length == 0))

Comments

0

I would be a little suspect of this line:

if ((zipcode.value == "ZipCode") || (zipcode.value.length == null) || (zipcode.value.length == "")) {

Try this instead:

if ((zipCode.value == null) || (zipcode.value== "") || (zipcode.value.length == 0)) {

1 Comment

zipcode.value.length will never return null. zipcode or zipcode.value might, but length is an int.
0

That error message usually indicates that you've tried to get or set a property or call a method of something that isn't an object, which tends to happen when a variable that you thought referred to an object is actuall null or undefined. That is, if someVariable is null or undefined then you can't say someVariable.someProperty.

If .getElementById() doesn't find a matching element it returns null, so in this line:

var zipcode = document.getElementById(sender.id.replace(/cv_/, "tb_"));

zipcode is potentially set to null, and if it is then all attempts to access properties of zipcode like zipcode.value and zipcode.style will fail.

If the parameter args comes in as null or undefined then attempting to set args.IsValid will fail, and similarly if the parameter sender is null or undefined then sender.id will fail.

So, if you have a variable that might be null you should test that before trying to do anything else with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.