Theme Graphic
Theme Graphic

Jonesy's Great Code Archive

This is for me, not you. But I hope you find it useful none-the-less. Suggestions or general comments are appreciated.
Posted on Tuesday, August 25, 2009 at 10:37 AM

Implementation of a asp customvalidator using regex

The following is an example of how to validate a field using the asp:customvalidator control. I am going to be validating a textbox to ensure that its entry is alphanumeric without any spaces.

This is the field to be validated...
<asp:TextBox id='PersonId' runat='server' />


Here is the asp:CustomValidator on the aspx page.
<asp:CustomValidator id='PersonIdCustomValidator' runat='server'
	Display='Dynamic'
	ControlToValidate='PersonId'
	EnableClientScript='true'
	ClientValidationFunction='ValidatePersonId'
	OnServerValidate='ValidatePersonId'
	Text='* PersonId must be alphanumeric without any spaces' />


Javascript implementation of the client-side validation.
function ValidatePersonId(source, args)
{	
	var re = new RegExp("^\\w*$");
	
	args.IsValid = (window.event.srcElement.value.match(re));
}


C# codebehind implementation of the server-side validation.
protected void ValidatePersonId(object source, ServerValidateEventArgs args)...

 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.