Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have a textbox & I want to paste only those element which is only numeric value not a character using javascript.
Posted
Updated 6-Jun-21 20:45pm
v2
Comments
Dholakiya Ankit 12-Aug-13 3:08am    
y? javascript filteredtextbox can be useful
Thanks7872 12-Aug-13 3:16am    
Paste or write? Why javascript only? Do you expect any other means also?

Try this:
JavaScript
// This code will bind a method to the paste operation, and in the method filters out the invalid chars.
$('#yourelement').bind('paste', function() {
    var el = this;
    setTimeout(function() {
        el.value = el.value.replace(/\D/g, '');
    }, 0);
});

* Note: this solution is using jQuery[^]

Cheers,
Edo
 
Share this answer
 
v3
Comments
Herbisaurus 12-Aug-13 3:20am    
*****
Joezer BH 12-Aug-13 3:22am    
Thank you Herbisaurus!
<input type="text" size="25" style="width: 60px" tabindex="13" maxlength="5" id="txtPostalCode">
onkeypress="return keyCheck(event,this,'NM','U','');" runat="server" />

/**
* Alphabetic and AlphaNumeric.
* Parameters: Object, "AL"/"AN"/"NM", "U"/"L", Special_Char_String.
**/
function keyCheck(objEvent, objObject, strType, strCase, strSpecialChars) {

var intKeycode, intLen;
intKeycode = objEvent.keyCode;
if (intKeycode == 13)
return true;
var strObjvalue = objObject.value;
if (!strSpecialChars)
strSpecialChars = "";
if (!strCase)
strCase = "";
if (!strType)
strType = "";

intLen = strSpecialChars.length;
if (intKeycode == 34 && strSpecialChars.indexOf('D') != -1) // for the quotes.
return true;
if (intKeycode == 39 && strSpecialChars.indexOf('S') != -1) // from the apostrophe.
return true;

if (strType == "NM") {
if (!((intKeycode >= 48 && intKeycode <= 57) ||
(intKeycode == 32))) {

var vcharAt = strSpecialChars.indexOf(String.fromCharCode(intKeycode))
if (vcharAt == -1)
event.keyCode = 0;
}
}
return true;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900