?
Solved

Range validatator HELP (String)

Posted on 2008-10-16
26
Medium Priority
?
454 Views
Last Modified: 2013-12-17
Hi,

I would like to set a control for two things

Username: (I would like this to have a min of 4 characters abd 10 max)

Password: (I would like this to have at least 1 number and min of 6 characters and max 15 in total)

Any idea how I can achieve this?

Regards,

introlux
0
Comment
Question by:introlux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
  • 5
  • +1
26 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 22729405
Does this need to be client side validation or serverside, you should generally do either both or serverside only as clientside only can be bypassed.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22729413
Use custom validator. It lets you perform the validation yourself so you could write code such as
if txtusername.text.length < 4 or txtusername.text.length > 10 then
   'error
end if
0
 
LVL 1

Expert Comment

by:MarkMyburgh
ID: 22729467
Hi
how much validation do you want to do in your application?
you could use the microsoft enterprise libary application block.
There is a StringLengthValidator  you can use :
sort of what the code would look like :
[StringLengthValidator(1, 50, Ruleset = "RuleSetA",
MessageTemplate = "First Name must be between 1 and 50 characters")]
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:introlux
ID: 22729557
I would just prefer to use the validation controls on client side. As its only a minor username and password. Not going to be used heavily.

Just needed something simple to do this. I am using c# and NOT vb.

Regards,

introlux
0
 
LVL 1

Expert Comment

by:MarkMyburgh
ID: 22729602
which framework 1, 2, 3, 3.5?
0
 
LVL 1

Expert Comment

by:MarkMyburgh
ID: 22729670
you could just implement the validating method on the control itself

private void textBox1_Validating(object sender, CancelEventArgs e)
{
if ((textBox1.Text.Length < 3) || (textBox1.Text.Length > 10))
{
//tell user he is a doofus
}
}
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22729753
You could use a regular expression validator.
0
 
LVL 1

Accepted Solution

by:
MarkMyburgh earned 1000 total points
ID: 22729792
sorry don't know why i thought this was a windows app:
below is an example of how to validate using the customer validator

<body>
    <form id="form1" runat="server">
    <div>
      
      <asp:TextBox id="TextBox1" runat="server" />
      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="TextBox1"
           ClientValidationFunction="PasswordLength"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Doofus!"          
           runat="server"/>
      <br /> 
      <asp:Button id="Button1"
           Text="Validate" 
           OnClick="ValidateBtn_OnClick" 
           runat="server"/>    
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
   function PasswordLength(source, clientside_arguments)
   {         
      if ((clientside_arguments.Value.Length < 3)  || ((clientside_arguments.Value.Length > 10))) <-- //could be wrong as i have not done java scripts in a while
      {
         clientside_arguments.IsValid=true;
      }
      else 
     {
	clientside_arguments.IsValid=false
      };
   }
</script>

Open in new window

0
 

Author Comment

by:introlux
ID: 22730348
Compiler Error Message: CS0117: 'ASP.testvalidation_aspx' does not contain a definition for 'ServerValidation'

Source Error:

 

Line 12:      
Line 13:       <asp:TextBox id="TextBox1" runat="server" />
Line 14:       <asp:CustomValidator id="CustomValidator1"
Line 15:            ControlToValidate="TextBox1"
Line 16:            ClientValidationFunction="PasswordLength"
 
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22730398
ServerValidation is the function which would be executed on the server when validating. Create this procedure in the codebehind on the server.
0
 

Author Comment

by:introlux
ID: 22731625
Compiler Error Message: CS0246: The type or namespace name 'CancelEventArgs' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 17:     }
Line 18:
Line 19:     private void ServerValidation(object sender, CancelEventArgs e)
Line 20:     {
Line 21:         if ((textBox1.Text.Length < 3) || (textBox1.Text.Length > 10))
 
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1000 total points
ID: 22731665
This is what you need
System.Web.UI.WebControls.ServerValidateEventArgs
0
 

Author Comment

by:introlux
ID: 22731697
Compiler Error Message: CS0138: A using namespace directive can only be applied to namespaces; 'System.Web.UI.WebControls.ServerValidateEventArgs' is a type not a namespace

Source Error:

 

Line 9:  using System.Web.UI.WebControls.WebParts;
Line 10: using System.Web.UI.HtmlControls;
Line 11: using System.Web.UI.WebControls.ServerValidateEventArgs;
Line 12:
Line 13: public partial class TestValidation : System.Web.UI.Page
 
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22731762
Mate, just replace this
private void ServerValidation(object sender, CancelEventArgs e)
with this
private void ServerValidation(object sender, System.Web.UI.WebControls.ServerValidateEventArgs e)
0
 
LVL 1

Expert Comment

by:MarkMyburgh
ID: 22732514
itolux, you come right there?
0
 

Author Comment

by:introlux
ID: 22732723
Compiler Error Message: CS0122: 'TestValidation.ServerValidation(object, System.Web.UI.WebControls.ServerValidateEventArgs)' is inaccessible due to its protection level

Source Error:

 

Line 12:      
Line 13:       <asp:TextBox id="TextBox1" runat="server" />
Line 14:       <asp:CustomValidator id="CustomValidator1"
Line 15:            ControlToValidate="TextBox1"
Line 16:            ClientValidationFunction="PasswordLength"
 
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22733017
make it friend instead of private.
0
 

Author Comment

by:introlux
ID: 22738586
what do you mean?
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1000 total points
ID: 22738613
change this
private void ServerValidation(object sender, System.Web.UI.WebControls.ServerValidateEventArgs e)
to this
public void ServerValidation(object sender, System.Web.UI.WebControls.ServerValidateEventArgs e)

if it still does not help, upload your full code here.
0
 

Author Comment

by:introlux
ID: 22738633
It compiles now but does not do anything!
<body>
    <form id="form1" runat="server">
    <div>
      
      <asp:TextBox id="TextBox1" runat="server" />
      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="TextBox1"
           ClientValidationFunction="PasswordLength"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Doofus!"          
           runat="server"/>
      <br /> 
      <asp:Button id="Button1" Text="Validate" runat="server"/>    
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
   function PasswordLength(source, clientside_arguments)
   {         
      if ((clientside_arguments.Value.Length < 3)  || ((clientside_arguments.Value.Length > 10))) <-- //could be wrong as i have not done java scripts in a while
      {
         clientside_arguments.IsValid=true;
      }
      else 
     {
	clientside_arguments.IsValid=false
      };
   }
</script>
 
public partial class TestValidation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    public void ServerValidation(object sender, System.Web.UI.WebControls.ServerValidateEventArgs e)
    {
        if ((TextBox1.Text.Length < 3) || (TextBox1.Text.Length > 10))
        {
            //tell user he is a doofus 
        }
    } 
}

Open in new window

0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1000 total points
ID: 22738677
remove this attribute
ClientValidationFunction="PasswordLength"


add a validation summary control to the website and configure it properly. What code have you written in the code behind? Post that code as well.
0
 

Author Comment

by:introlux
ID: 22738715
Here you go
=======================================================================
 
Front code
 
=======================================================================
 
<body>
    <form id="form1" runat="server">
    <div>
      
      <asp:TextBox id="TextBox1" runat="server" />
      <asp:CustomValidator id="CustomValidator1"
           ControlToValidate="TextBox1"
           OnServerValidate="ServerValidation"
           Display="Static"
           ErrorMessage="Doofus!"          
           runat="server" ValidationGroup="Test"/>
      <br /> 
      <asp:Button id="Button1" Text="Validate" runat="server" ValidationGroup="Test"/>    
    </div>
 
<asp:ValidationSummary ID="ValidationSummary1"
ShowMessageBox="true" ValidationGroup = "Test"
ShowSummary="false"
HeaderText="You must enter a value in the following fields:"
EnableClientScript="true"
runat="server"/>
    
    </form>
</body>
</html>
<script type="text/javascript">
   function PasswordLength(source, clientside_arguments)
   {         
      if ((clientside_arguments.Value.Length < 3)  || ((clientside_arguments.Value.Length > 10))) <-- //could be wrong as i have not done java scripts in a while
      {
         clientside_arguments.IsValid=true;
      }
      else 
     {
	clientside_arguments.IsValid=false
      };
   }
</script>
 
=======================================================================
 
Code behind
 
=======================================================================
 
public partial class TestValidation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    public void ServerValidation(object sender, System.Web.UI.WebControls.ServerValidateEventArgs e)
    {
        if ((TextBox1.Text.Length < 3) || (TextBox1.Text.Length > 10))
        {
            //tell user he is a doofus 
        }
    } 
}

Open in new window

0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1000 total points
ID: 22738724
Replace
 //tell user he is a doofus
with
e.IsValid=False;
0
 

Author Comment

by:introlux
ID: 22738734
It seems like its working now - Only one more thing, when you do no enter anything, it does not show the error.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1000 total points
ID: 22738769
Set ValidateEmptyText to true.
0
 

Author Closing Comment

by:introlux
ID: 31506657
Thanks for the help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question