string 如何使用javascript在文本框中设置最小和最大字符长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21172991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:12:38  来源:igfitidea点击:

How to set min and max character length in a textbox using javascript

stringtextboxcharactermaxmin

提问by cala

If I have a text and I only want to allow the user enter text between 5 and 10 characters long, how do I do this using javascipt?

如果我有一个文本并且我只想允许用户输入 5 到 10 个字符长的文本,我该如何使用 javascipt 来做到这一点?

I have tried using mixand maxfunctions but they only works for numeric data.

我试过使用mixmax函数,但它们只适用于数字数据。

回答by rboling

You could do something like this:

你可以这样做:

`

`

function checkLength(){
    var textbox = document.getElementById("textbox");
    if(textbox.value.length <= 10 && textbox.value.length >= 5){
        alert("success");
    }
    else{
        alert("make sure the input is between 5-10 characters long")
    }
}
</script>
<input type="text" id="textbox"></input>
<input type="submit" name="textboxSubmit" onclick="checkLength()" />
`

回答by njfife

You need to use the maxlength attribute for input fields, something like this should do it:

您需要对输入字段使用 maxlength 属性,应该这样做:

<input name="myTextInput" type="text" maxlength="5"></input>

回答by hemanth

You can use "maxlength" attribute to not allow more than x characters & do validation using javascript for min length.

您可以使用“maxlength”属性不允许超过 x 个字符并使用 javascript 验证最小长度。

see this example: http://jsfiddle.net/nZ37J/

看这个例子:http: //jsfiddle.net/nZ37J/

HTML

HTML

<form id="form_elem" action="/sdas" method="post">
   <input type="text" id="example" maxlength="10"></input>
   <span id="error_msg" style="color:red"></span>
   <input type="button" id="validate" value="validate"></input>
</form>

Javascript:

Javascript:

$("#validate").click(function(){
    var inputStr = $("#example").val();
    if(inputStr.length<5)
        $("#error_msg").html("enter atleast 5 chars in the input box");
    else
        $("#form_elem").submit();      
})

回答by Marc F

I did a bit of a mix of the samples above trying to make it as simple as possible and avoid unnecessary alerts. Script:

我对上面的示例进行了一些混合,试图使其尽可能简单并避免不必要的警报。脚本:

function checkLength(){
    var textbox = document.getElementById("comment");
    if(textbox.value.length <= 500 && textbox.value.length >= 5){
        return true;
    }
    else{
        alert("Your comment is too short, please write more.");
        return false;
    }
}

code in the form field would be: onsubmit="return checkLength();">

表单字段中的代码将是: onsubmit="return checkLength();">

And the text area in the form itself:

以及表单本身的文本区域:

<label for="comment">*Comment:</label>
<textarea name="comment" id="comment" rows="4" cols="40" required="required"></textarea>

hope this helps!

希望这可以帮助!

回答by Delbin Thomas

<input name="myTextInput" type="text" minlength="5" maxlength="10"></input>

This would do the trick if you do not want to trouble with js.

如果你不想给 js 带来麻烦,这会奏效。