2

I cant seem to get a string length from a value in a form textfield Here's the code for my function, its getting passed the parameter of the textfields name

  function validate(thing)
  {

  var location = document.getElementById(thing);

  var cardst = location.value;
  window.alert (cardst);
  cardst = String(cardst);
  window.alert (cardst.lenght);

the first alert works, it alerts whatever I type into the textfield, but the second one is always undefined. As you can see I did cast it as a string but I get get undefined.. ANy ideas??

2
  • 2
    Typo: cardst.lenght should be cardst.length; Also, not sure of the reason for creating a new string. Commented Jun 4, 2012 at 22:08
  • Using "location" as variable name is a bad idea. How do you call your function? Commented Jun 4, 2012 at 22:09

2 Answers 2

15

That would be because you misspelled length :-)

Use this instead:

cardst.length;   // <- 'th', not 'ht'
Sign up to request clarification or add additional context in comments.

Comments

2

You have a syntax error. It's length not lenght

   function validate(thing)
  {

  var location = document.getElementById(thing);

  var cardst = location.value;
  window.alert (cardst);
  cardst = String(cardst);
  window.alert (cardst.lenght);
                           ^ /*should read window.alert(cardst.length)*/

1 Comment

try ctrl+shift+j on most browsers

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.