469,898 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

restrict the user entering nonalphanumeric keys?

I want to restrict the user entering nonalphanumeric keys using Javascript
onKeypress.
One exception is that I want to allow the user to enter spaces.

How can I do this?

Thanks in Advance.

Jun 13 '07 #1
1 1783
Howdy,

Two variants (both restrict to alphanumeric or nonalphanumeric):
<asp:TextBox runat="server"
onKeyPress="return restrict(this, true);" ID="TextBox1" />
<asp:TextBox runat="server"
onKeyPress="return restrict(this, false);" ID="TextBox2" />

<script type="text/javascript">
function restrict(input, alphanumeric)
{
var regex = alphanumeric ? /[a-zA-Z0-9_ ]/ : /[^a-zA-Z0-9_ ]/;

var code = event.which ? event.which : event.keyCode;

return regex.test(String.fromCharCode(code));
}
</script>

hope this helps

--
Milosz
"C" wrote:
I want to restrict the user entering nonalphanumeric keys using Javascript
onKeypress.
One exception is that I want to allow the user to enter spaces.

How can I do this?

Thanks in Advance.
Jun 13 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by vijay21 | last post: by
10 posts views Thread by sconeek | last post: by
3 posts views Thread by divaof_7 | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.