我有这段代码。根据这里的密钥代码 http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000520.html
此代码应该可以工作,但出于某种原因,我将这些字符视为真实。
eiadfghcb。
function validate(event) {
var keycode = event.keyCode;
if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57) && (keycode < 96 || keycode > 105)) {
return false;
}
}
HTML:
<asp:TextBox ID="txtImp" runat="server" Height="23px" Width="80" onkeypress="return validate(event)" onkeyup="calc()"/>
答案 0 :(得分:1)
以下代码适合您:
function validate(event) {
var code = event.code;
if (typeof code !== "undefined") {
var
codeBeginning = code.substr(0, code.length - 1);
if (code === "Period" || code === "NumpadDecimal" || code === "Backspace" || ((codeBeginning === "Digit" || codeBeginning === "Numpad") && !parseInt(code.substr(code.length - 1)).isNaN())) { // key pressed is one of the "."-keys, the "Backspace"-key or one of the number-keys.
return true;
}
return false;
}
var keyCode = event.which || event.keyCode;
if (keyCode === 8 || keyCode === 46 || (keyCode >= 48 && keyCode <= 57)) {
return true;
}
return false;
}
当按下的键既不是小数点键也不是!(keycode == 8 || keycode == 46)
键时,if语句BACKSPACE
中的第一个条件确实会被评估为真。
然而,第二和第三个条件将相互冲突。这可以通过以下示例显示:
用户按下Numpad 2
- 键(在我的情况下)导致50
。此值符合第二个条件,因为50
高于48
且低于57
,但由于50
低于96
,因此不符合第三个条件true
。
由于第二个和第三个条件都必须导致false
,并且两个中总会有一个导致KeyBoardEvent.keyCode
,因此代码永远不会按照您的意图执行。< / p>
我之前的回答是KeyBoardEvent.keyCode
不可靠,导致无法在我的机器上捕获正确的密钥。
即使我现在无法重现此问题,我仍然建议您在绝对必要时仅使用KeyBoardEvent.keyCode
(因为KeyBoardEvent.which
的文档确实声明它是特定于实现的) ,并尽可能使用KeyBoardEvent.keyCode
。
由于KeyBoardEvent
在很大程度上依赖于浏览器的实施,我选择尽可能多地使用KeyBoardEvent.which
。
然而,由于这两个属性已被弃用,我还使用了KeyBoardEvent.code
来确保解决方案符合最新的KeyBoardEvent.code
规范。
因此,我的解决方案在可用时使用KeyBoardEvent.code
,因为它不被弃用或特定于实现。如果KeyBoardEvent.which
不可用,则使用KeyBoardEvent.keyCode
,因为它与KeyBoardEvent.which
更加一致。最后,如果KeyBoardEvent.keyCode
(如旧浏览器中的情况,例如Internet Explorer 8),则必须使用Enter the authorization code: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Traceback (most recent call last):
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 345, in _make_request
self._validate_conn(conn)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 844, in _validate_conn
conn.connect()
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\packages\urllib3\connection.py", line 326, in connect
ssl_context=context)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\packages\urllib3\util\ssl_.py", line 324, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\ssl.py", line 401, in wrap_socket
_context=self, _session=session)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\ssl.py", line 808, in __init__
self.do_handshake()
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\ssl.py", line 1061, in do_handshake
self._sslobj.do_handshake()
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\ssl.py", line 683, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\adapters.py", line 423, in send
timeout=timeout
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 630, in urlopen
raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\googlesamples\assistant\auth_helpers\__main__.py", line 54, in <module>
main()
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\click\core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\click\core.py", line 697, in main
rv = self.invoke(ctx)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\click\core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\click\core.py", line 535, in invoke
return callback(*args, **kwargs)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\googlesamples\assistant\auth_helpers\__main__.py", line 48, in main
creds = credentials_flow_interactive(client_secrets, scope)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\googlesamples\assistant\auth_helpers\__init__.py", line 47, in credentials_flow_interactive
flow.run_console()
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\google_auth_oauthlib\flow.py", line 358, in run_console
self.fetch_token(code=code)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\google_auth_oauthlib\flow.py", line 235, in fetch_token
**kwargs)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests_oauthlib\oauth2_session.py", line 221, in fetch_token
verify=verify, proxies=proxies)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 535, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests_oauthlib\oauth2_session.py", line 360, in request
headers=headers, data=data, **kwargs)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Benji\AppData\Local\Programs\Python\Python36\lib\site-packages\requests\adapters.py", line 497, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
。
答案 1 :(得分:0)
看看你的第三个条件:
keycode < 96 || keycode > 105 //Where keycode is NOT between 96-105
现在查看您输入的字符的ASCII代码:
a: 97
b: 98
c: 99
d: 100
e: 101
f: 102
g: 103
h: 104
现在显而易见的是,为什么您的代码失败了 - 您已经包含了一个非常明确地忽略了您声称“不能工作”的字符的条件。
说到keyCode
,您将遇到一些跨浏览器问题。出于这个原因,您可能需要考虑同时检查keyCode
和/或 charCode
,因为每个浏览器都适用于一组特定的浏览器。确保我们获得一致的价值的一种简单方法是执行以下操作:
var keycode = event.keyCode || event.charCode;
如果event.keyCode
无法正常工作,则会使用charCode
。
如果您只是想忽略我指出的问题的条件,那么只需删除它:
if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57)) {
return false;
}
话虽如此,你的问题并没有说明你的愿望......它只是说你所拥有的&#34;对于提及的字符&#34; 不起作用。
作为旁注,如果我没有指出你的代码不完全是...... 友好,那我就会失职,因为缺少一个更好的词。解决这个问题的一种优雅方法是用命名函数替换条件列表,因此目的和结果更加明显,如下所示:
<强>为:强>
if (sunny || not raining and warm || not(cloudy and raining) || not cold)
不可强>
if (weatherIsNice(...))
适用于您的情况,可能类似于
function characterIsAllowed(keycode) {
if (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57) && (keycode < 96 || keycode > 105)) {
return true;
} else {
return false;
}
}
function validate(event) {
var keycode = event.keyCode || event.charCode;
if (characterIsAllowed(keycode)) {
return false;
}
}
或者,进一步简化......
function characterIsAllowed(keycode) {
return (!(keycode == 8 || keycode == 46) && (keycode < 48 || keycode > 57) && (keycode < 96 || keycode > 105))
}
function validate(event) {
var keycode = event.keyCode || event.charCode;
return !characterIsAllowed(keycode);
}