如何查看表单字段? [英] How to check a form field?
问题描述
我是Javascript的新手,想检查用户在填写输入字段时提供的文本是否只有数字
。 br />
有什么想法吗?
谢谢
L
I am a newbie with Javascript and would like to check if a text
provided by a users , during filling in an input field , has digits
only.
Any idea?
Thank you
L
推荐答案
PythonistL写道:
PythonistL wrote:
我是Javascript的新手,想检查用户是否提供了文本
,在填写输入字段时,只有数字
。
有什么想法吗?
谢谢
L
I am a newbie with Javascript and would like to check if a text
provided by a users , during filling in an input field , has digits
only.
Any idea?
Thank you
L
类似于:
if(isNAN(document.getElementById(''myTextField'')。valu e))
{
代码,如果不是数字;
}
其他
{
代码如果是数字;
}
Something like:
if (isNAN(document.getElementById(''myTextField'').valu e))
{
code if not number;
}
else
{
code if is number;
}
Jeff Paffett于2006年5月15日在comp.lang.javascript中写道 :
Jeff Paffett wrote on 15 sep 2006 in comp.lang.javascript:
PythonistL写道:
PythonistL wrote:
>我是Javascript的新手,想检查用户在填写输入字段时提供的文字是否有数字<只有。
任何想法?
谢谢
L
>I am a newbie with Javascript and would like to check if a text
provided by a users , during filling in an input field , has digits
only.
Any idea?
Thank you
L
类似的东西:
if(isNAN(document.getElementById(''myTextField'')。valu e))
{
代码,如果不是数字;
}
其他
{
代码如果是数字;
}
Something like:
if (isNAN(document.getElementById(''myTextField'').valu e))
{
code if not number;
}
else
{
code if is number;
}
不是数字
与
不同字符串只有数字
尝试:
if(/[^\d]/.test(document.getElementById(''myTextField''))value))
代码,如果不是数字;
}否则{
代码如果是数字;
}
-
Evertjan。
荷兰。
(请将x''es更改为圆点在我的电子邮件中)
"Not a number"
is not the same as
"String has digits only"
Try:
if (/[^\d]/.test(document.getElementById(''myTextField'').value )) {
code if not number;
} else {
code if is number;
}
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)
Evertjan。写道:
Evertjan. wrote:
Jeff Paffett于2006年5月15日在comp.lang.javascript中写道 :
PythonistL写道:
PythonistL wrote:
我是Javascript的新手,想检查一下用户提供的文本
,在填写输入字段时,只有数字
。
有什么想法吗?
谢谢
L
I am a newbie with Javascript and would like to check if a text
provided by a users , during filling in an input field , has digits
only.
Any idea?
Thank you
L
类似于:
if(isNAN(document.getElementById(''myTextField'')。valu e))
{
代码,如果不是数字;
}
其他
{
代码如果是数字;
}
Something like:
if (isNAN(document.getElementById(''myTextField'').valu e))
{
code if not number;
}
else
{
code if is number;
}
不是数字
不一样
"字符串只有数字
尝试:
if(/ [^ \d] /。试验(的document.getElementById( '' myTextField将 ).value)){
代码,如果不是数字;
}否则{
代码如果是数字;
}
"Not a number"
is not the same as
"String has digits only"
Try:
if (/[^\d]/.test(document.getElementById(''myTextField'').value )) {
code if not number;
} else {
code if is number;
}
如果你想在
输入时阻止除数字之外的任何东西,你可以附加一个函数给
文本字段的onkeypress事件,对于非数字键击返回false。这样他们就可以在输入时显示
。
在w3schools.com上有一个相反的例子。我不知道
如何清理代码,但它看起来像这样:
< script type =" text / javascript"> ;
函数noNumbers(e)
{
var keynum
var keychar
var numcheck
if(window.event)// IE
{
keynum = e.keyCode
}
else if(e.which)// Netscape / Firefox / Opera
{
keynum = e.which < br $>
}
keychar = String.fromCharCode(keynum)
numcheck = / \d /
返回numcheck。 test(keychar)
}
< / script>
< form>
输入一些文字(不允许的数字):
< input type =" text" onkeypress =" return noNumbers(event)" />
< / form>
也许一些经验丰富的开发人员可以修复任何不良编码。
If you wanted to prevent anything but digits being accepted while
typing, you could attach a function to the onkeypress event of the
textfield and return false for non-digit keystrokes. This way they
wouldn''t show up when typed.
There''s an example of the opposite on w3schools.com. I don''t know how
clean the code is, but it looks like this:
<script type="text/javascript">
function noNumbers(e)
{
var keynum
var keychar
var numcheck
if(window.event) // IE
{
keynum = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which
}
keychar = String.fromCharCode(keynum)
numcheck = /\d/
return numcheck.test(keychar)
}
</script>
<form>
Type some text (numbers not allowed):
<input type="text" onkeypress="return noNumbers(event)" />
</form>
Maybe some more seasoned developers could fix any bad coding.
>
-
Evertjan。
荷兰。
(请更改x''es到我的电子邮件地址中的点数)
>
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)
这篇关于如何查看表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!