如何验证粘贴事件中的文本字段 [英] how to validate text field on paste event

查看:89
本文介绍了如何验证粘贴事件中的文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有数字文本字段,在粘贴一些文本时应验证该文本.
有人可以建议我这样做的方法吗?

我添加了以下代码,以便在用户输入数字时起作用,

< telerik:RadNumericTextBox ID ="_ rdtxtScanNumber" runat =服务器" MinValue ="0" MaxValue ="999999999999999999" NumberFormat-GroupSeparator =" NumberFormat-KeepNotRoundedValue ="False" NumberFormat-DecimalSeparator =" NumberFormat-DecimalDigits ="0 "Width =" 250px>
< ClientEvents OnKeyPress ="ValidateNumber"/>
</telerik:RadNumericTextBox>

并且在ValidateNumber函数中,当用户输入最后一个键时,它将验证数字并可以正常工作.但是在粘贴的情况下将不起作用


I have numeric textfield, on paste of some text it should validate the text.
Can any one suggest me a way for this.

I have added below code to work when a user enter number,

<telerik:RadNumericTextBox ID="_rdtxtScanNumber" runat="server" MinValue="0" MaxValue="999999999999999999" NumberFormat-GroupSeparator="" NumberFormat-KeepNotRoundedValue="False" NumberFormat-DecimalSeparator=" " NumberFormat-DecimalDigits="0" Width="250px" >
<ClientEvents OnKeyPress="ValidateNumber" />
</telerik:RadNumericTextBox>

and in ValidateNumber function when user enter last key, it will validate the number and works fine. but in case of pasting it won''t work
can any one suggest a way to handle this.

推荐答案

您可以使用"onPaste"事件在客户端进行验证.试试这个链接.
希望对您有所帮助!
You can use "onPaste" event to put validation at client side. Try this link.
Hope it helps!


您可以在aspx页面的文本框声明中使用以下内容.

OnPaste="JavaScript:return RestrictCopyPaste();"

并也使用此javascript函数.

You can use the following on textbox declaration in aspx page.

OnPaste="JavaScript:return RestrictCopyPaste();"

And use this javascript function also.

function RestrictCopyPaste()
{
    var num = clipboardData.getData(''Text'')
    if(parseInt(num) != num)
        return false;
}



经过测试,可以在所有情况下使用.



Tested and worked for all scenarios.


处理TextChanged事件,并确保没有粘贴任何非数字字符-如果有的话,将其删除.
Handle the TextChanged event, and ensure no non-numeric characters have been pasted - if they have, remove them.


这篇关于如何验证粘贴事件中的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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