2016-09-15 33 views
0

我有一個表單中的數字字段,我想接受多個句點。該字段將用於輸入軟件版本號。我只能輸入一段時間的代碼。一旦我在場上添加兩個句點,我會收到錯誤消息。這是我正在使用的代碼。 我正在使用表單生成器。並可以添加Javascript和CSS。我不能編輯的HTML或PHPjquery允許數字段接受多個句點

$('.decimal input').numeric({allow:"."}); 
<div class="decimal"> 
    <label class="description" >Version Number (must be a number) <span class="required">*required</span></label> 
    <div> 
     <input class="element text small" type="text" value=""> 

    </div> 
    </div> 
+1

你會得到什麼錯誤? –

+0

我在添加兩個句點時遇到的錯誤是「此字段必須是數字」 – nicban

+0

他正在使用[numeric.js](https://github.com/SamWM/jQuery-Plugins/tree/master/numeric/) –

回答

0

也許使用正則表達式?它僅匹配數字和任意數量的點,但不匹配字符串的開頭或結尾。另外兩個點不允許彼此相鄰。

var regex = /^(\*|\d+(\.\d+)*(\.\*)?)$/i; 
if (regex.test($('.decimal input'))) { 
    // valid number 
} 
+1

這忽略了一個事實,即OP正試圖使用​​一些名爲'numeric'的東西(可能是一個插件)。 –

+0

@cale_b這是必要的,因爲插件根本不是爲此目的而設計的。 – Barmar

+0

@cale_b他可能會使用日期選擇器來驗證數字。 – Barmar