如何只在文本框中粘贴数值? [英] How to paste only numeric value in textbox?

查看:100
本文介绍了如何只在文本框中粘贴数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,我想只使用javascript粘贴那些只是数字值而不是字符的元素。

Hi, I have a textbox & I want to paste only those element which is only numeric value not a character using 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 );
});
('#yourelement').bind('paste', function() { var el = this; setTimeout(function() { el.value = el.value.replace(/\D/g, ''); }, 0); });



* 注意: th解决方案是使用 jQuery [ ^ ]



干杯,

Edo


* Note: this solution is using jQuery[^]

Cheers,
Edo


< input type =textsize =25style =width:60pxtabindex =13maxlength =5id =txtPostalCode>

onkeypress =return keyCheck(event,this,'NM','U',''); runat =server/>



/ **

*字母和AlphaNumeric。

*参数:Object,AL/AN/NM,U/L,Special_Char_String。

** /

函数keyCheck(objEvent,objObject,strType,strCase,strSpecialChars){



var intKeycode,intLen;

intKeycode = objEvent.keyCode;

if(intKeycode == 13)

返回true;

var strObjvalue = objObject.value;

if(! strSpecialChars)

strSpecialChars ="" ;;

if(!strCase)

strCase ="";

if(!strType)

strType ="" ;;



intLen = strSpecialChars.length;

if(intKeycode == 34&& strSpecialChars.indexOf('D')!= -1)//为报价。

返回true;

if(intKeycode == 39&& strSpecialChars.indexOf('S')!= -1)//来自撇号。

返回true;



if(strType = =" NM"){

if(!((intKeycode> = 48&& intKeycode< = 57)||

(intKeycode == 32 ))){$ /


var vcharAt = strSpecialChars.indexOf(String.fromCharCode(intKeycode))

if(vcharAt == -1)

event.keyCode = 0;

}

}

返回true;

}
<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;
}


这篇关于如何只在文本框中粘贴数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆