Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom Validator Controls. 2

Status
Not open for further replies.

Kalisto

Programmer
Joined
Feb 18, 2003
Messages
997
Location
GB
I have a custom validator that I want to use to check the length of a password (minimum of 4 caracters)

my html is
Code:
<asp:CustomValidator id="cvPasswordLength" 
runat="server" ErrorMessage="Password Must be at Least 4 Characters Long" ControlToValidate="tbPassword"
OnServerValidate="ServerValidatorPWLength">*</asp:CustomValidator>

But my code behind validator is never called.
This is the code that sets up the call

Code:
this.cvPasswordLength.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.ServerValidatorPWLength);

Is there anything obviously wrong there?

(I intend adding in Client Side Validation as well, but want to get the server side working 1st)
 
Unless your doing this just to try out custom validators I'd use a regular expression validator this...
Code:
<asp:regularexpressionvalidator id="val" runat="server" validationexpression="^[\w\d]{4,}$" ErrorMessage="Password Must be at Least 4 Characters Long" ControlToValidate="tbPassword" />

HTH

Rob


Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Cheers, that would work for 1 thing i need to do, but i also want to check for invalid characters such as :/\<> etc, you'd recommend a regular expression for that as well?
 
I'll call it "open source" I've been wanting to make a few improvements on it (like registering a single client-side function that accepts and processes minimum length as one of the arguments), but here's my mimimum length validator control's source:

Code:
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;

namespace ClubCrossControls
{
	/// <summary>
	/// Summary description for MinimumLengthValidator.
	/// </summary>
	[DefaultProperty("MinimumLength"),
	ToolboxData("<{0}:MinimumLengthValidator MinimumLength=0 ErrorMessage=\"Does Not Meet Minimum Length Requirement\" Text=* runat=server></{0}:MinimumLengthValidator>")]
	public class MinimumLengthValidator : CustomValidator
	{
		private int minLength;

		public MinimumLengthValidator()
		{}

		protected override void OnInit(System.EventArgs e)
		{
			base.OnInit (e);

			//register server-side script
			this.ServerValidate += 
				new ServerValidateEventHandler(this.TextValidate);
		}


		override protected void OnPreRender(System.EventArgs e)
		{
			base.OnPreRender (e);
			
			//register client-side validator
			if( this.EnableClientScript )
			{
				//if blank, use default
				if( this.ClientValidationFunction == "" )
					this.ClientValidationFunction = this.ID + "validateLength";
				//if default is not overridden
				if( this.ClientValidationFunction == this.ID + "validateLength" )
					Page.RegisterClientScriptBlock( this.ID +"MinScript", GetValidationScript() );
			}
		}

		private string GetValidationScript()
		{
			StringBuilder sb = new StringBuilder();

			sb.Append( "<SCRIPT LANGUAGE=\"JavaScript\">" );
			sb.Append(	"function " );
				sb.Append( this.ID );
					sb.Append( @"validateLength(oSrc, args){
									args.IsValid = (args.Value.length >=" );
										sb.Append( MinimumLength.ToString() );
					sb.Append( @");
					}
				</SCRIPT>" );

			return sb.ToString();
		}

		protected void TextValidate(object source, ServerValidateEventArgs args)
		{
			args.IsValid = (args.Value.Length >= MinimumLength);
		}

		/// <summary>
		/// The minimum length, in characters, for the control.
		/// </summary>
		[Category("Custom Properties"),
				Description("The minimum length of the field in characters")]
		public int MinimumLength
		{
			get
			{
				return minLength;
			}
			set
			{
				minLength = value;
			}
		}
	}
}

 
Kalisto:

If you want to restrict characters then you can be more explicit in the expression as to what is allowed.

^[a-zA-Z0-9]{4,}$

for example will only allow lowercase and uppercase letters and numbers in the field. If you wanted to allow other characters you could add them in like...

^[a-zA-Z0-9_.]{4,}$

Alternatively you can use a hat at the start of the grouping to say "not" allowed like this....

^[^%]{4,}$

This would allow any characters other than % to pass through. Doing it this way it is much harder to be sure you have included every disallowed character so in most cases I wouldnt do this.

Here's a link to the MSDN documentation on regular expression if you want to find out more...


HTH

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
BB

I'll check out the validator later and let you know any feedback.

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top