0

I have a form:

<form id="userForm" action="xpto">
     <input type="text" name="form[data_nascimento_beneficiario_1]" value />
     <input type="submit" value="Continuar" />
</form>

Now on a script file i try to get the lenght of the field for validation:

$("#userForm").submit(function (e) {
     alert($("input[name="form[data_nascimento_beneficiario_1]"]).length());
});

But i always get "TypeError: Cannot call method 'length' of null",

I guess the problem is on the name to have "form[]", but that is nothing i can change :(

Can anyone help me please?

Regards to all.

4 Answers 4

3

A few issues there:

  1. Your code as quoted actually has a syntax error and so wouldn't give you the error you've said, you've ended your JavaScript string just after the word name.

  2. When the attribute value you're testing contains anything but the letters A-Z, it's best to put it in quotes.

  3. length is "th" at the end, not "ht"

  4. ...and in jQuery it's a property, not a method, so you don't want () after it

  5. The name you're using in your selector (data_nascimento_baneficiario_1) doesn't match the name in the form (data_nascimento_beneficiario_1), note the "a" at the beginning of "baneficiario" in the selector.

All together: Live Example | Source

alert($('input[name="form[data_nascimento_beneficiario_1]"]').length);

(There, I've used ' to put quotes around the selector string and " to put quotes around the attribute value. The other way around also works.)

Sign up to request clarification or add additional context in comments.

6 Comments

I have also tryed that out, but no i give's me the same error.
@NunoSimões: Your code as quoted can't give you the error you've said, so it sounds like perhaps you're not editing the right file or some such. Also note #4 in the above (hit refresh if you don't see a numbered list).
@NunoSimões: I was doing a live example and found a fifth issue.
ty for your help. The code i have is really extensive, so i have made a short version of it, sorry about that. The 'th' have been changed, sorry for the typo. The '()' part has also been tryed out without it, but i get always the same error "TypeError: Cannot call method 'length' of null", it like the element does not exist, but it does!
@NunoSimões: You shouldn't get that error message from jQuery. Are you sure you're using jQuery and not Prototype or MooTools (which also use $)? Because jQuery will always give you back a non-null result. It may be an empty set, but it won't be null. Prototype, MooTools, and the DOM itself will give you null if they can't find something, but not jQuery, which is set-based.
|
0

You have a typo: .lenght() - it should be length. Also, be careful about your quote wrapping.

Change your line to this:

alert($("input[name='form[data_nascimento_baneficiario_1]']).length);

Comments

0

You have a typo, and length is not a function() so there is no need of invoking it with ()

Change this

lenght()

to

length

DEMO

Comments

0

Note sure why that is not working but a workaround would be

$("input").filter(function(){ 
     return this.name = 'form[data_nascimento_beneficiario_1]'}
).val().length

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.