Es veidoju tīmekļa lapu, kurā man ir ievades teksta lauks, kurā es gribu atļaut tikai ciparu rakstzīmes, piemēram, (0,1,2,3,4,5...9) 0-9.
Kā to izdarīt, izmantojot jQuery?
Piezīme: Šī ir atjaunināta atbilde. Zemāk minētie komentāri attiecas uz veco versiju, kurā bija neskaidrības ar atslēgu kodiem.
Pamēģiniet paši [JSFiddle][1].
Šim nolūkam nav dzimtās jQuery implementācijas, bet jūs varat filtrēt teksta <input>
ievades vērtības, izmantojot šādu inputFilter
spraudni (atbalsta Copy+Paste, Drag+Drop, tastatūras īsceļus, konteksta izvēlnes operācijas, neievadāmos taustiņus, caret pozīciju, dažādus tastatūras izkārtojumus un visas pārlūkprogrammas kopš IE 9):
// Restricts input for the set of matched elements to the given inputFilter function.
(function($) {
$.fn.inputFilter = function(inputFilter) {
return this.on("input keydown keyup mousedown mouseup select contextmenu drop", function() {
if (inputFilter(this.value)) {
this.oldValue = this.value;
this.oldSelectionStart = this.selectionStart;
this.oldSelectionEnd = this.selectionEnd;
} else if (this.hasOwnProperty("oldValue")) {
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
}
});
};
}(jQuery));
Tagad varat izmantot inputFilter
spraudni, lai instalētu ievades filtru:
$(document).ready(function() {
$("#myTextBox").inputFilter(function(value) {
return /^\d*$/.test(value); // Allow digits only, using a RegExp
});
});
Šeit ir vēl daži ievades filtru piemēri, kas varētu būt noderīgi:
return /^-?\d*$/.test(value); // Integer
return /^\d*$/.test(value); // Integer >= 0
return /^\d*$/.test(value) && // Integer >= 0 and <= 500
(value === "" || parseInt(value) <= 500);
return /^-?\d*[.,]?\d*$/.test(value); // Float (accept both '.' and ',' as decimal separator)
return /^-?\d*[.,]?\d{0,2}$/.test(value); // Currency (i.e. at most two decimal places)
return /^[a-z]*$/i.test(value); // A-Z only
return /^[0-9a-f]*$/i.test(value); // Hexadecimal
Atzīmējiet, ka jums joprojām jāveic validācija servera pusē!
Patiesībā jQuery tam nav nepieciešams, to pašu var izdarīt arī ar tīru JavaScript. Skatiet šo atbildi vai izmēģiniet to paši [JSFiddle][4].
HTML 5 ir vietējais risinājums ar <input type="number">
(sk. specifikāciju), taču jāņem vērā, ka pārlūkprogrammu atbalsts atšķiras:
step
, min
un max
.e
un E
rakstzīmes šajā laukā. Skatiet arī šo jautājumu.Izmēģiniet paši vietnē w3schools.com.
$(document).ready(function() {
$("#txtboxToFilter").keydown(function(event) {
// Allow only backspace and delete
if ( event.keyCode == 46 || event.keyCode == 8 ) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if (event.keyCode < 48 || event.keyCode > 57 ) {
event.preventDefault();
}
}
});
});
Avots: http://snipt.net/GerryEng/jquery-making-textfield-only-accept-numeric-values
Varat izmantot šo JavaScript funkciju:
function maskInput(e) {
//check if we have "e" or "window.event" and use them as "event"
//Firefox doesn't have window.event
var event = e || window.event
var key_code = event.keyCode;
var oElement = e ? e.target : window.event.srcElement;
if (!event.shiftKey && !event.ctrlKey && !event.altKey) {
if ((key_code > 47 && key_code < 58) ||
(key_code > 95 && key_code < 106)) {
if (key_code > 95)
key_code -= (95-47);
oElement.value = oElement.value;
} else if(key_code == 8) {
oElement.value = oElement.value;
} else if(key_code != 9) {
event.returnValue = false;
}
}
}
Un jūs varat saistīt to ar savu teksta lodziņu šādi:
$(document).ready(function() {
$('#myTextbox').keydown(maskInput);
});
Es izmantoju iepriekš minēto, un tas darbojas perfekti, un tas ir piemērots dažādiem pārlūkiem. Turklāt tas nav atkarīgs no jQuery, tāpēc varat to saistīt ar teksta lodziņu, izmantojot iebūvētu JavaScript:
<input type="text" name="aNumberField" onkeydown="javascript:maskInput()"/>