限制表单输入文本字段中允许的字符数
如何限制或限制用户在文本框中最多只能输入五个字符?
下面是作为我的表单一部分的输入字段:
<input type="text" id="sessionNo" name="sessionNum" />
它是否使用 maxSize 之类的东西或类似的东西?
How do I limit or restrict the user to only enter a maximum of five characters in the textbox?
Below is the input field as part of my form:
<input type="text" id="sessionNo" name="sessionNum" />
Is it using something like maxSize or something like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
最大长度:
但是,这可能会或可能不会受到您的处理程序的影响。您可能还需要使用或添加另一个处理程序函数来测试长度。
maxlength:
However, this may or may not be affected by your handler. You may need to use or add another handler function to test for length, as well.
最简单的方法是:
所以..将此属性添加到您的控件中:
The simplest way to do so:
So.. Adding this attribute to your control:
将以下内容添加到标题中:
Add the following to the header:
让它变得更简单
,并使用警报来显示已使用最大字符数。
Make it simpler
and use an alert to show that max chars have been used.
根据 w3c,MAXLENGTH 属性的默认值是无限个。因此,如果您没有指定最大值,用户可以多次剪切并粘贴圣经并将其粘贴到您的表单中。
即使您确实将 MAXLENGTH 指定为合理的数字,请确保在处理之前仔细检查服务器上提交的数据的长度(使用 php 或 asp 等),因为无论如何,绕过基本的 MAXLENGTH 限制都很容易
According to w3c, the default value for the MAXLENGTH attribute is an unlimited number. So if you don't specify the max a user could cut and paste the bible a couple of times and stick it in your form.
Even if you do specify the MAXLENGTH to a reasonable number make sure you double check the length of the submitted data on the server before processing (using something like php or asp) as it's quite easy to get around the basic MAXLENGTH restriction anyway
输入中可包含的最大字母数量为 5。
the maximum amount of letters that can be in the input is 5.
Maxlength
将接受作为输入的最大字符数。
maxlength 属性指定元素中允许的最大字符数。
Maxlength W3 学校
Maxlength
The maximum number of characters that will be accepted as input.
The maxlength attribute specifies the maximum number of characters allowed in the element.
Maxlength W3 schools
以下代码包含一个计数...
The following code includes a counted...
我总是这样做:
输入:
I always do it like this:
Input:
您可以使用
<输入类型=“文本”maxlength=“9”>
或
对于数字
或者
用于电子邮件
验证将会显示
You can use
<input type = "text" maxlength="9">
or
<input type = "number" maxlength="9">
for numbersor
<input type = "email" maxlength="9">
for emailvalidation will show up
不要为旧的解决方案而烦恼。
这是您需要的:
Don't bother with old solutions.
Here's what you need:
晚了,但如果你想要一个完整的证明方法来限制数字或字母,这只是 javascript 并且还限制字符长度:
更改
.slice
之后的第二个数字来设置字符数。与maxlength
相比,这对我来说效果更好。仅数字:
仅字母:
完整示例:
Late to the party, but if you want a full proof way to restrict numbers or letters that is simply javascript and also limits length of characters:
Change the second number after
.slice
to set the how many characters. This has worked much better for me thenmaxlength
.Just Numbers:
Just Letters:
Full example:
使用 maxlenght="字符数"
Use maxlenght="number of charcters"