【问题标题】:Validating numeric value in text field does not give desired result验证文本字段中的数值不会给出所需的结果
【发布时间】:2013-08-26 04:13:02
【问题描述】:

在javascript中验证一个文本字段值,该值可能是正整数或带一个小数点和一个小数点后的数字:

123
1
12345
233.2
1212.2
1.1

是有效数字和

1.222
1.33
-89789
-3

是无效数字。

我已经应用了这个但没有得到想要的结果

  function CheckOneDecimal(txtbox) {
             if (txtbox.value.length > 0)
             {
                 if (isNaN(txtbox.value)) {
                     txtbox.value = "";
                     alert("Please enter number with one decimal places");
                     txtbox.focus();
                     return;
                 }
                 else if (parseFloat(txtbox.value) > 0)
                 {
                     txtbox.value = "";
                     alert("Please enter number with one decimal places. eg. 8.1");
                     txtbox.focus();
                     return;
                 }

                 var oRegExp = /^\s*\d+\.\d{1}\s*$/;
                   if (oRegExp.test(txtbox.value))
                     { }
                   else {

                         txtbox.value = "";
                         alert("Please enter number with one decimal places");
                         txtbox.focus();
                     }
               }
         }

【问题讨论】:

  • “但没有得到想要的结果” - 你得到了什么做什么?
  • 我只是在验证后得到空的 txtbox。如果 txt 的值为 3.444 或 32323,则仅在其值为 4.5 时才有效。但是通过以下解决方案可以正常工作,谢谢您的回复

标签: javascript regex validation


【解决方案1】:

这个正则表达式应该这样做:

/^\d*\.?\d$/

演示: http://jsbin.com/ovaVAfU/1/edit

【讨论】:

  • 感谢您的大力帮助。非常感谢。它工作完美
  • 你能告诉我在哪里可以学习这个正则表达式开发吗?谢谢
  • 这将使.2 有效。我不确定它是否需要。
【解决方案2】:

我愿意:

/^\d+(?:\.\d)?$/

解释:

The regular expression:

(?-imsx:^\d+(?:\.\d)?$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多