1jaws
asked on
validation for lenght of the textbox
I have credit card textbox that it has maxleght is set to 16 but i need to put a validation on that if user enters less than 16 digits it should complain.. which validation is good for that?
required and then you'll need a custom
[aspx]
<asp:TextBox ID="txtCred" runat="server" MaxLength="16" />
<asp:RequiredFieldValidato r
ID="RequiredFieldValidator 1" ControlToValidate="txtCred " runat="server" ErrorMessage="Must enter Credit Card number!"></asp:RequiredFie ldValidato r>
<asp:CustomValidator ControlToValidate="txtCred " ID="CustomValidator1"
runat="server" ErrorMessage="Must be 16 characters long!"
onservervalidate="CustomVa lidator1_S erverValid ate"></asp :CustomVal idator>
[C#]
protected void CustomValidator1_ServerVal idate(obje ct source, ServerValidateEventArgs args) {
args.IsValid = args.Value.Length == 16;
}
<asp:TextBox ID="txtCred" runat="server" MaxLength="16" />
<asp:RequiredFieldValidato
ID="RequiredFieldValidator
<asp:CustomValidator ControlToValidate="txtCred
runat="server" ErrorMessage="Must be 16 characters long!"
onservervalidate="CustomVa
[C#]
protected void CustomValidator1_ServerVal
args.IsValid = args.Value.Length == 16;
}
You can use a custom validator or you can do it manually with textbox.text.length
ASKER
is there anyway to not user code behind? or have to for that?
why don't you want to use the code behind??
you can do it inline...
<script language="C#" runat="server">
protected void CustomValidator1_ServerVal idate(obje ct source, ServerValidateEventArgs args) {
args.IsValid = args.Value.Length == 16;
}
</script>
Many people write code this way. You will need the extra .cs file.
you can do it inline...
<script language="C#" runat="server">
protected void CustomValidator1_ServerVal
args.IsValid = args.Value.Length == 16;
}
</script>
Many people write code this way. You will need the extra .cs file.
ASKER
what is actually differrent between writing protected void in Javascript and codebehind. which one is better? also do I need to add anthing in CompareValidator1 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!