2011-05-13 15 views
2

誰能告訴我如何檢查在文本框中輸入量(印度貨幣)是否有效或不使用正則表達式印度貨幣驗證表達?定期對使用JavaScript

我有幾個條件..

  1. 量不應含有超過1個個小數,但可以有一個小數點。
  2. 如果有小數點,那麼它後面應該跟一個或多個數字。
  3. 金額應該只有數字,最多一位小數。
  4. 如果我輸入的數量是10.000,那麼它不應該被接受,因爲它在小數點後有3個連續的零。但應該接受56.8906。
  5. 如果量爲零(即0123)開始它不應該被接受,但0.0應該接受
+2

爲什麼'56.8906'當'10.000'是不是有效? – JohnP 2011-05-13 05:28:05

+0

如果56.8906是美元,你怎麼能給56美元和'89.06美分? – 2011-05-13 05:43:46

+0

並非所有的世界都在使用美元。這是盧比。具有3個連續零的 – mplungjan 2011-05-13 05:45:00

回答

3
^(?:0|[1-9]\d*)(?:\.(?!.*000)\d+)?$ 

應該做你想要什麼。

說明:

^   # Start of string. 
(?:  # Try to match... 
0  # either a 0 
|   # or 
[1-9]\d* # an integer number > 0, no leading 0 allowed. 
)   # End of integer part. 
(?:  # Try to match... 
\.  # a decimal point. 
(?!  # Assert that it's not possible to match 
    .*000 # any string that contains 000 from this point onwards. 
)  # End of lookahead assertion. 
\d+  # Match one or more digits. 
)?  # End of the (optional) decimal part 
$   # End of string. 

在JavaScript:

curRegExp = /^(?:0|[1-9]\d*)(?:\.(?!.*000)\d+)?$/; 
+0

謝謝你蒂姆...它的工作正常...非常感謝您的回覆.. – Raghu 2011-05-13 06:49:09