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?
ASP.NET

Avatar of undefined
Last Comment
1jaws
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;
   }
Avatar of JacobBushong
JacobBushong
Flag of United States of America image

You can use a custom validator or you can do it manually with textbox.text.length
Avatar of 1jaws
1jaws
Flag of United States of America image

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
1jaws
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of 1jaws
1jaws
Flag of United States of America image

ASKER

Thank you!
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo