在文本框上验证 [英] validation on textbox

查看:81
本文介绍了在文本框上验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#在ASP.NET上开发一个应用程序,我只需要传递文本和数字.请帮我做.
在此先谢谢您.

I am developing an application on ASP.NET in C# where I need to pass only text and numbers. Please help me to do this.
Thanks in advance.

推荐答案

尝试以下操作

Try the following

<asp:TextBox ID="txtCode" runat="server" Width="200px"                         Enabled="true" OnKeyPress="return restrictCharacters(event);"></asp:TextBox>



以及下面的JavaScript函数



and the below JavaScript function

function restrictCharacters(e) {
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    var alphaOnly = /[A-Za-z0-9]/g;
    if (code == 27) { this.blur(); return false; }
    if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40) {
        if (character.match(alphaOnly)) {
            return true;
        } else {
            return false;
        }
    }
}



希望这会有所帮助.



Hope this helps.


这篇关于在文本框上验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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