2015-05-25 29 views
0

在我的php中,我使用preg_match來驗證輸入文本。用於貨幣值(例如5000或5000.00)的文本框或可以接受空白。preg_match only nummber,允許空格和貨幣格式

$pattrn="/^[0-9]{10}$/"; 

通過使用它可以驗證數字只。 如何使用preg_match過濾此輸入?

+0

檢查了這一點http://stackoverflow.com/questions/10371476/preg-match-decimals-not-working –

+1

嘗試'$ pattrn =「/ ^(?:\ d +( ???:\ \ d +))$ /「'。 –

+0

嗨,有陌生人@AmalMurali ;-) –

回答

3

您可以使用:

$pattrn = "/^(?:\d+(?:\.\d+)?)?$/"; 

說明:

/^    # Assert position at the beginning of the string 
(?:   # Begin a non-capturing group 
    \d+  # Match one or more digits from 0-9 
    (?:  # Begin a non-capturing group 
     \.\d+ # Match a dot (.) followed by one more digits from 0-9 
    )?   # Make the capturing group optional 
)?    # Make the capturing group optional 
$/    # Assert positon at the end of the string 

Regex101 Demo