2012-09-26 30 views
0

我想驗證一個文本框有使用下面的代碼數值,但我不知道該過濾條件那裏的數字,我的代碼是:如何驗證使用jquery的必需和數字文本框?

function validateRequiredNumeric(Control, msgInfo) { 
    var ControlId = $('#' + Control); 
    var msgInfoId = $('#' + msgInfo); 
    //testing regular expression 
    var a = ControlId.val(); 
    var filter = /[^\d]/; 
    alert(a.length); 
    if (a.length != 0) { 
     if (filter.test(a)) { 
      msgInfoId.text(''); 
      ControlId.css({ 'border': '1px solid green' }); 
      return true; 
     } else { 
      //msgInfoId.css({ 'color': 'red', 'font-size': '12px', 'font-style': 'italic' }); 
      msgInfoId.text("Numeric Onlydd fdf dfd"); 
      ControlId.css({ 'border': '1px solid red' }); 
      return false; 
     } 
    } 
    else { 
     //msgInfoId.css({ 'color': 'red', 'font-size': '12px', 'font-style': 'italic' }); 
     msgInfoId.text("You can't leave this empty."); 
     ControlId.css({ 'border': '1px solid red' }); 
     return false; 
    } 

} 

請幫我..我已經嘗試了「[^0-9]「這也..

+0

你只是想驗證一個文本框具有數值,應該是一個必填字段正則表達式? –

+1

'/ [^ \ d] /'除了數字以外的所有數字都是否定的字符類 – fcalderan

+0

@RananSharma是的,我想要你解釋的一樣 – Ram

回答

1

的正則表達式應該是:

僅適用於數字/^[\d]+$/

對於任何非數字/^[^\d]+$/

我不知道,因爲你的正則表達式嘗試是對非你在你的問題是什麼意思數字。

另一個回答讓我想想看,如果你希望用戶使用小數,你應該加入一些字符的情況下...

對於小數/^[\d]+[,.]?[\d]*$/ 要求小數點前至少一個數字。

0

嘗試Texottela plugin,能夠使用一個numeric()功能,則可以提供丟失焦點時運行的回調,並在文本框中的值是不是有效數。

0

我相信你正在尋找的期限是regular expression

這應做到:

var filter = /^\d*$/; 

克拉^符號將錨十進制字符串 的$符號的開始將小數點固定到字符串的末尾 和\d*匹配一個或多個十進制值

如果其他e是字符串中的任何非小數,正則表達式將返回false

0

請按照this。它很好地解釋了你在這裏尋找的東西。

0

你不需要爲

var a = Number($.trim(controlID)); //or parseInt($.trim(controlID)) 
    if (!isNaN(a)) { 
     //a is valid number 
    } else { 
     //a is not a number or is empty 
    }