限制用户只能通过javascript在TextBox中输入1)数字,2)10位数字,3)字母数字 [英] Restrict user to enter only 1) numbers, 2) 10 digits, 3) alphanumeric in TextBox by javascript

查看:56
本文介绍了限制用户只能通过javascript在TextBox中输入1)数字,2)10位数字,3)字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)如何限制用户只使用javascript在文本框中输入数字。

2)如何限制用户使用javascript在文本框中输入10位数(手机号码)。

3)或者如何限制用户使用javascript在文本框中仅输入字母数字。

1) How to restrict user to enter only numbers in textbox using javascript.
2) How to restrict user to enter only 10 digits(mobile number) in textbox using javascript.
3) Or how to restrict user to enter only alphanumeric in textbox using javascript.

推荐答案

所有这些场景都可以通过使用这个小的javascript框架轻松实现。你只需要调用这些函数而不需要其他任何东西。



一个用于常见验证方案的小型Javascript框架。 [ ^ ]



它具有所有必需的功能,您甚至可以根据自己的需要更改它们。



您可能希望将它们与文本框的MAxlength属性和一些自定义正则表达式结合使用。
All these scenarios can easily be handed by using this small javascript framework. You will only need to call the functions and nothing else.

A Tiny Javascript Framework for Common Validation Scenarios.[^]

It has all the required functions, you can even change them as per your needs.

YOu might want to use them in combination with MAxlength property of textboxes and some custom regular expressions.


1)& 3)使用AJAXControlToolkit的FilteredTextBoxExtender控件。

2)只有10位数:

使用RegularExpressionValidator控件!

只需设置ValidationExpression属性控制为:

1) & 3) Use AJAXControlToolkit's FilteredTextBoxExtender Control.
2) Only 10 digits:
Use the RegularExpressionValidator Control for that!
Just set the ValidationExpression property of the Control as:
ValidationExpression="\d{10}"



这将完成任务!


This will do the task!


你可以用两种方式做到这一点

1.javascript

2.ajax





使用javascript



you can do it in two ways
1.javascript
2.ajax


using javascript

<script type="text/javascript">

   function numbersonly()
   {
   var data=document.getElementById('<%=TextBox1.ClientID %>').value;
   var filter=/^[a-zA-Z0-9]+


这篇关于限制用户只能通过javascript在TextBox中输入1)数字,2)10位数字,3)字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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