2013-10-02 32 views
-3

我需要只允許數字和文本費爾德一個點像56.50,67.00,78,98.05,0.25,0.50等只允許在文本字段中的數字和一個點(浮點值)與keyup事件

$('#psp').keyup(function() { 
if number or dot 
............. 
............. 
else 
alert('oops... just entered an invalid character'); 
}); 

我會很感激,如果有人可以幫助:)

+2

使用正則表達式,有什麼問題? – Barmar

+0

可能重複的[jQuery:什麼是限制「數字」的最佳方式 - 只輸入文本框? (允許小數點)(http://stackoverflow.com/questions/891696/jquery-what-is-the-best-way-to-restrict-number-only-input-for-textboxes-all) – undefined

回答

1

你可以使用這個表達式:

^(?=\D*\d)\d*(\.\d+)?$ 
+1

您需要使點可選。否則,他們將永遠無法在點之前鍵入字符。 – Barmar

+0

@Barmar:是的,謝謝,糾正。 – anubhava

1

你可以試試下面它使用正則表達式代碼:

var num = "56.50"; 
if(/^(?=.)[0-9]?[0-9]?(\.[0-9][0-9]?)?$/.test(num)) { 
    // 
} 
+0

例如,它不會匹配'.75'。 – anubhava

+0

更新我的回答,以匹配0.75和類似 – Rajesh

+0

嗯,現在它不會匹配'75' – anubhava

相關問題