Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  34] [ 3]  / answers: 1 / hits: 53007  / 11 Years ago, sun, june 16, 2013, 12:00:00

This is something that's driving me nuts:



I have this code and it works: I am trying to learn JavaScript before becoming addicted to JQuery. My sample project involves getting the value of the text-box, and validating according to it's length. the name of the form is membership.



Example: This works:



function validateForm()
{
var element = document.membership;

if(element.txtName.value == )
{
element.txtName.className = alert;
}
else
{
element.txtName.className = ;
}
}


But this doesn't:



function validateForm()
{
var element = document.membership;
var nameLenght = element.txtName.value.lenght;

if(nameLenght < 1)
{
element.txtName.className = alert;
}
else
{
element.txtName.className = ;
}
}


Just an FYI: I am new to JavaScript but very familiar with the syntax. I just want to learn the basics and move up.



I even read some solutions on here but feel I am simply sinking deeper.



Thanks for your help.


More From » javascript

 Answers
8

May be it is just because of typo in length here:



element.txtName.value.lenght;


must be element.txtName.value.length;.


If you want it to run every time user presses key , then look here: How to check string length with JavaScript


[#77607] Thursday, June 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
billosvaldor

Total Points: 601
Total Questions: 113
Total Answers: 113

Location: Iceland
Member since Sat, Sep 17, 2022
2 Years ago
;