文本框的自定义验证 [英] Custom validation for textbox
问题描述
我有一个文本框应该接受用户ID,它应该以你开头,然后是7个数字。
可以任何一个请帮帮我。
I have a text box which should accept the userid in such a way that it should start with an "u" and then 7 numbers.
can any one help me out please.
推荐答案
那么你可以使用RegularExpressionValidator,它是ASp的控制以及Regex的组合....
如下所示。
Well You can use RegularExpressionValidator which is a control of ASp along with combination of Regex....
Like below.
<asp:RegularExpressionValidator ID="validator1" runat="server" BackColor="Red" ControlToValidate="ControlID" ValidationExpression="[u]{1}[0-9]{7}" ErrorMessage="ValidationMessageHere" >
</asp:RegularExpressionValidator>
[u] {1} [0-9] {7} 将允许以'u'开头的字符串后跟7位数...
如果你想让用户输入1到7之间的数字,你可以使用以下数字。 >
[u] {1} [0-9] {0,7}
它会允许
u12
u123
u1234
..... u1234567
希望这可以解决您的疑问... :)
[u]{1}[0-9]{7} will allow a string starting with 'u' followed by 7 digits...
if you want to allow user to enter numbers counting between 1 and 7 dn you can use the following..>
[u]{1}[0-9]{0,7}
it will allow
u12
u123
u1234
..... u1234567
Hope this can solve your query... :)
<asp:TextBox ID="txt_userid" runat="server" />
<asp:CustomValidator ID="CustomValidator" runat="server" EnableClientScript="true"
ErrorMessage="Userid should start with letter u followed with 7 digits"
ClientValidationFunction="validateuserid"
ControlToValidate="txt_userid" Display="Dynamic" >
</asp:CustomValidator>
< script type =text / javascript>
函数validateuserid(sender,args){
var Userid = document.getElementById('txt_userid')。value;
if(Userid.length == 8){
if(Userid.substring(0,1)=='u'){
args.IsValid = true; < br $> b $ b}
else {
var regex ='^ [0-9] {7}
<script type="text/javascript">
function validateuserid(sender, args) {
var Userid = document.getElementById('txt_userid').value;
if (Userid.length == 8) {
if (Userid.substring(0, 1) == 'u') {
args.IsValid = true;
}
else {
var regex = '^[0-9]{7}
';
var str = Userid.substring(1,8)
if(str.search(regex)== -1){
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
}
else {
args.IsValid = false;
}
}
';
var str = Userid.substring(1, 8)
if (str.search(regex) == -1) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
}
else {
args.IsValid = false;
}
}
这篇关于文本框的自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!