Link to home
Start Free TrialLog in
Avatar of 1jaws
1jawsFlag for United States of America

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?
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

required and then you'll need a custom
[aspx]

<asp:TextBox ID="txtCred" runat="server" MaxLength="16" />
      <asp:RequiredFieldValidator
         ID="RequiredFieldValidator1" ControlToValidate="txtCred" runat="server" ErrorMessage="Must enter Credit Card number!"></asp:RequiredFieldValidator>
       <asp:CustomValidator ControlToValidate="txtCred" ID="CustomValidator1"
           runat="server" ErrorMessage="Must be 16 characters long!"
           onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
[C#]

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) {
      args.IsValid = args.Value.Length == 16;
   }
You can use a custom validator or you can do it manually with textbox.text.length
Avatar of 1jaws

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_ServerValidate(object source, ServerValidateEventArgs args) {
      args.IsValid = args.Value.Length == 16;
   }
    </script>

Many people write code this way. You will need the extra .cs file.

Avatar of 1jaws

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
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 1jaws

ASKER

Thank you!