使用JavaScript获取文本框值或内容的长度 [英] Getting the length of a textbox value or content using JavaScript

查看:99
本文介绍了使用JavaScript获取文本框值或内容的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我发疯:

我有这段代码,它的工作原理是:在尝试沉迷于JQuery之前,我正在尝试学习JavaScript.我的示例项目涉及获取文本框的值,并根据其长度进行验证.表单的名称为 会员身份 .

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 = "";
    }
}

仅供参考:我不熟悉JavaScript,但是对语法非常熟悉.我只想学习基础知识并继续前进.

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.

感谢您的帮助.

推荐答案

可能只是因为

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

element.txtName.value.lenght;

必须为 element.txtName.value.length; .

如果您希望它在用户每次按键时运行,请查看此处:
如何使用JavaScript检查字符串长度

这篇关于使用JavaScript获取文本框值或内容的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆