[英]Simple regex in JavaScript works in all browsers but Safari
I have a couple of forms with input fields of type number don't seem to have any validation applied by Safari (on Mac OS X Mavericks). 我有几种形式的输入字段为数字类型的表单,Safari似乎没有应用任何验证(在Mac OS X Mavericks上)。 That is, if one types letters and hits submits, in every browser a little message appears that you need to put numbers in the field - except Safari.
也就是说,如果输入字母并点击了,那么在每个浏览器中都会出现一则小信息,提示您需要在字段中输入数字-Safari除外。 Oh, and Firefox too on this platform.
哦,这个平台上也是Firefox。
So, I decided I needed to add validation to my JavaScript code that is handling these form values. 因此,我决定需要在处理这些表单值的JavaScript代码中添加验证。 I put in a regular expression to reject anything that is not a single-digit number:
我输入了一个正则表达式来拒绝任何不是一位数字的东西:
if ( !/^([0-9])$/.test(value) ) {
alert ("Please enter a number");
return;
}
In Firefox, this behaves as I hoped. 在Firefox中,它的行为符合我的期望。 If you enter any letters, or anything other than a single digit, the alert is displayed.
如果输入任何字母或除一位数字以外的任何数字,则显示警报。
On Safari, if you enter two or more digits, the alert is displayed too, but if you enter non-digit characters, it is not. 在Safari上,如果输入两个或多个数字,也会显示警报,但如果输入非数字字符,则不会显示。 It acts as if it happily accepts the input, but if I then further add an alert box in an else block below the above to show me what value is, that doesn't get displayed either.
它的行为就好像它愉快地接受了输入一样,但是如果我然后在上面下面的else块中进一步添加一个警报框,以向我展示什么值,那么该值也不会显示。 There are no errors in the JavaScript console.
JavaScript控制台中没有错误。 It's like it just does nothing.
就像什么都不做。
I've stared at the regex a bunch, though it's about as simple as it can be. 我盯着正则表达式一堆,尽管它尽可能简单。 I'm pretty flummoxed.
我很糊涂。 Any ideas, anyone?
有什么想法吗?
I don't really like regexes. 我真的不喜欢正则表达式。 They are always so error prone and you sometimes don't consider all possible matches.
它们总是很容易出错,有时您不会考虑所有可能的匹配项。 Why don't you use something like this:
你为什么不使用这样的东西:
if (parseInt("15", 10) !== NaN)
alert("is numeric");
else
alert("please enter a number");
Your regex tests true for a string that is a single digit and false (and enters your negated if condition) otherwise. 您的正则表达式对单个数字的字符串测试为true ,否则为false (并输入否定的条件)。 Must it be only one digit or one or more digits?
只能是一位数字还是一位或多位数字?
Add a + to specify one or more numeric values. 添加+以指定一个或多个数字值。
/^([0-9]+)$/
Also do you need or are you using the grouping (parenthesis around the digits)? 您还需要还是正在使用分组(数字周围的括号)?
/^[0-9]+$/
Misc ways to validate integer .. 验证整数的其他方法..
// Integers including 0: following values will return true (0, 1, 2 .., 65, etc.)
/^(?:0|[1-9]\d*)$/.test(value);
// Integers, zero not allowed
/^[1-9]\d*$/.test(value);
// Test whether the value is an integer or not (+ sign try to cast the value into a number)
if(Number.isInteger(+value)){/* ... */}
User friendly solution .. 用户友好的解决方案..
Few months ago, I had to format a user input that had to be either an integer or a float number ; 几个月前,我不得不格式化必须为整数或浮点数的用户输入; Formatting process is triggered on keypress.
格式化过程在按键上触发。
This is what I achieved Fiddle here 这就是我在这里取得小提琴的目的
Allowed formats 允许的格式
Hope it will help .. 希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.