我有一個簡單的textbox
用戶輸入號碼。
jQuery是否有一個isDigit
函數,允許我顯示一個警告框,如果用戶輸入數字以外的東西?檢查輸入的號碼是否是jquery中的數字
該字段也可以有小數點。
我有一個簡單的textbox
用戶輸入號碼。
jQuery是否有一個isDigit
函數,允許我顯示一個警告框,如果用戶輸入數字以外的東西?檢查輸入的號碼是否是jquery中的數字
該字段也可以有小數點。
我會建議使用正則表達式:
var intRegex = /^\d+$/;
var floatRegex = /^((\d+(\.\d *)?)|((\d*\.)?\d+))$/;
var str = $('#myTextBox').val();
if(intRegex.test(str) || floatRegex.test(str)) {
alert('I am a number');
...
}
或用單正則表達式按照@Platinum Azure的建議:
var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
var str = $('#myTextBox').val();
if(numberRegex.test(str)) {
alert('I am a number');
...
}
該數字也可以有小數。 5001.10應該可以接受 – Omnipresent 2009-08-13 15:28:51
@Omnipresent - 對不起,但你確實說過isDigit,我會編輯。 – karim79 2009-08-13 15:31:06
應該指定。 :-)你可以使用像這樣的正則表達式來表示所有(很多)可能的數字:'/^[+-]?\d+(\.\d+)?([eE][+-]?\d+)? $ /'這將允許小數,符號,甚至科學記數法結束,如果你想。您可以根據需要刪除各種部分(如果您需要幫助確定哪部分是什麼,但它應該相當直觀),則可以使用Google Perl風格的正則表達式。 – 2009-08-13 15:31:17
jQuery的是一組JavaScript函數,對不對?所以你可以使用JavaScript的正則表達式支持來驗證字符串。如果你喜歡,你可以把它放在一個jQuery回調函數中,因爲它們只需要匿名聲明的函數體,而且函數仍然是JavaScript。
值驗證不會是jQuery的的責任。你可以使用純JavaScript來做到這一點。我想到的兩種方法是:
/^\d+$/.match(value)
Number(value) == value
既然你的第二個例子使用==而不是===,那麼不會在右側輸入強制,導致表達式始終爲真? – 2009-08-13 15:39:50
不知道爲什麼沒有人提出這個答案。我用數字(價值)....它的完美。 – 2016-07-10 06:50:05
@systemPAUSE提及用戶輸入和「數字」讓我覺得這個值是一個字符串和== ==的原因。 – 2016-07-11 02:19:48
忘記正則表達式。 JavaScript有這樣的內置函數:isNaN()
:
isNaN(123) // false
isNaN(-1.23) // false
isNaN(5-2) // false
isNaN(0) // false
isNaN("100") // false
isNaN("Hello") // true
isNaN("2005/12/12") // true
只是把它就像這樣:
if (isNaN($("#whatever").val())) {
// It isn't a number
} else {
// It is a number
}
isNaN不會確保您的輸入不包含空格。 isNaN(「42」)=== false – 2009-08-13 15:38:34
您可以使用類似於以下內容修剪空白:YouValue.replace(/^\ s \ s * /,'').replace(/ \ s \ s * $ /,'' ); 更多修剪實現:http://blog.stevenlevithan.com/archives/faster-trim-javascript – doomspork 2009-08-13 15:41:59
有趣的方法,但個人而言,我認爲正則表達式比雙重否定更清晰(不是非數字是一個數字)。儘管我傾向於將正則表達式放入一個很好的實用函數isDigit()中。 – 2009-08-13 15:47:02
使用jQuery的validation插件,你可以做這樣的事情,假設形式被稱爲形式並且要驗證的值被稱爲nrInput
$("form").validate({
errorElement: "div",
errorClass: "error-highlight",
onblur: true,
onsubmit: true,
focusInvalid: true,
rules:{
'nrInput': {
number: true,
required: true
}
});
這也處理十進制值。
有一種更簡單的方法來檢查變量是否是整數。你可以使用$ .isNumeric()函數。例如
$.isNumeric(10); // true
這將返回true,但如果您將一個字符串替換爲10,您將得到false。
我希望這對你有用。
以下腳本可用於檢查值是否爲有效整數。
function myFunction() {
var a = parseInt("10000000");
if (!isNaN(a) && a <= 2147483647 && a >= -2147483647){
alert("is integer");
} else {
alert("not integer");
}
}
String.prototype.isNumeric = function() {
var s = this.replace(',', '.').replace(/\s+/g, '');
return s == 0 || (s/s);
}
使用
'9.1'.isNumeric() -> 1
'0xabc'.isNumeric() -> 1
'10,1'.isNumeric() -> 1
'str'.isNumeric() -> NaN
$(document).ready(function() {
$("#cust_zip").keypress(function (e) {
//var z = document.createUserForm.cust_mobile1.value;
//alert(z);
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
$("#errmsgzip").html("Digits Only.").show().fadeOut(3000);
return false;
}
});
});
您還可以添加解釋而不是發佈代碼嗎? – namezero 2015-07-03 06:46:15
var yourfield = $('fieldname').val();
if($.isNumeric(yourfield)) {
console.log('IM A NUMBER');
} else {
console.log('not a number');
}
JQUERY DOCS:
所以你的意思是*號*和*不*數字? – 2014-09-16 19:33:52