[英]javascript user's input validation
I'm creating a program in which I need to validate user input.我正在创建一个需要验证用户输入的程序。
The User should be able to write float numbers and some letters like 'k' for thousand or 'm' for million.用户应该能够写浮点数和一些字母,如“k”代表千或“m”代表百万。 Allowed are: digits, and only one letter from the list(g,k,m,n,u) and only one '.'
允许的是:数字,列表中只有一个字母(g,k,m,n,u)和只有一个“。” for decimal numbers.
对于十进制数。 If user puts different sign, two letters from the list, two dots, follows letter by another sign - nothing displays in textbox.
如果用户放置不同的符号,列表中的两个字母,两个点,字母后面跟着另一个符号 - 文本框中不显示任何内容。
What i did so far is not working at all.到目前为止我所做的根本不起作用。 I cannot even create a list with allowed signs.
我什至无法创建带有允许标志的列表。 How can i solve this?
我该如何解决这个问题? I've seen almost every page on the web about regexp..
我几乎看过 web 上关于正则表达式的每一页。
function signFilter(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 = /[0-9GMkmpu.]/;
return numcheck.test(keychar);
}
http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeydown Here is sample code for input. http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onkeydown这是输入的示例代码。 Nothing i try gives resonable result.
我没有尝试给出合理的结果。 (of course, i removed '.'from return).
(当然,我从返回中删除了'.')。
Your regex is just testing the presence of one of the chars into [
and ]
.您的正则表达式只是在
[
和]
中测试其中一个字符的存在。 What you are trying to do is testing for the presence of any other char in the string.您要做的是测试字符串中是否存在任何其他字符。
I suggest you had a ^
sign like: [^0-9GMkmpu.]
, so you will be able to detect any wrong char in the string.我建议您使用
^
符号,例如: [^0-9GMkmpu.]
,这样您就可以检测到字符串中的任何错误字符。
With the regex you wrote something like: aaaaaaaaakhhhhhhhhh will return yes.使用正则表达式,您可以编写如下内容:aaaaaaaaaakhhhhhhhh 将返回是。
From the code you provided, it looks like you are testing the string one character at a time.从您提供的代码看来,您一次测试一个字符的字符串。 The code you have to only validates that correct letters are inputted, but as a whole, the final string created from the keypresses may not be in the format you expect.
您只需要验证输入正确字母的代码,但作为一个整体,从按键创建的最终字符串可能不是您期望的格式。
For example, a string 123.123.321.m
are all valid characters, but the final string is not valid.例如,字符串
123.123.321.m
都是有效字符,但最后的字符串无效。
Basically, you need to add validation that checks the final string inputted.基本上,您需要添加验证以检查输入的最终字符串。
A regexp like this could help you in validating the final string input.像这样的正则表达式可以帮助您验证最终的字符串输入。
numcheck = /^[0-9]*\.?[0-9]+[gkmnu]?$/
This regexp says这个正则表达式说
/^[0-9]*[\.]*[0-9]*[Mkmpu]*$/
This one is quite flexible and handles sereval cases such as ".57m" "57" "57m" "57.m" "57.57m".这个非常灵活,可以处理诸如“.57m”“57”“57m”“57.m”“57.57m”之类的案例。
This works这有效
http://jsfiddle.net/mplungjan/KvJQx/ http://jsfiddle.net/mplungjan/KvJQx/
window.onload=function() {
document.getElementById("t1").onkeypress=function(e) {
var keyNum = (window.event)?window.event.keyCode:e.which;
var keyChar = String.fromCharCode(keyNum);
return /[0-9GMkmpu\.]/.test(keyChar);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.