简体   繁体   English

JavaScript中的简单正则表达式可用于除Safari之外的所有浏览器

[英]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]+$/

fiddle 小提琴

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 允许的格式

  • .80 .80
  • 0.80 0.80
  • 15.80 15.80
  • 1500 1500

Hope it will help .. 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript表单提交不能在Safari中使用,并且可以在所有其他浏览器中使用? - javascript Form submit not working in safari and works in all other browsers? 预加载页面可在Safari之外的所有主要浏览器中使用 - Preloading page works in all major browsers but Safari jQuery条件在Safari中的所有浏览器中均适用 - Jquery condition works in all browsers except safari 在“ Safari”以外的所有浏览器上均可显示“小时和分钟”。 JavaScript日期 - Display “hours and minutes” works on all browsers except safari. JavaScript Date 表单提交不能在野生动物园中使用并且可以在所有其他浏览器中使用? - Form submit not working in safari and works in all other browsers? 按钮不会关闭 Safari 中的 window。 适用于所有其他浏览器 - button does not close window in Safari. Works for all other browsers 链接可用于除Safari(PHP代码)以外的所有浏览器 - Link works in all browsers except safari (php code) Regex-除safari外的所有移动浏览器的用户代理 - Regex - User Agent for all mobile browsers apart from safari Javascript功能适用于Edge以外的所有浏览器 - Javascript function works in all browsers except Edge Javascript 适用于除 Chrome 以外的所有浏览器 - Javascript works in all browsers except Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM