Using Regular Expression To Validate TextBox

If you want a textbox to only allow its content to be a string matches any alphanumeric string with spaces. You could use the RegularExpressionValidator Control, set its ControlToValidate property to the TextBoxID, and set its ValidationExpression property to ^[A-Za-z0-9,\s]+$

HTML CODE

<asp:RegularExpressionValidator id="RegularExpressionValidator1" style='Z-INDEX: 109; LEFT: 192px; POSITION: absolute; TOP: 96px'
    runat="server" ErrorMessage="RegularExpressionValidator" ValidationExpression="^[A-Za-z0-9,\s]+$" ControlToValidate="TextBox1"></asp:RegularExpressionValidator>

if you don't want to allow a space, you should change the validationexpression property to ^[a-zA-Z0-9]+$

I have found a good reference for regular expressions, check out the below link

Regular Expressions

Best Regards, 

HC

 

Published 19 April 2007 02:17 AM by haissam

Comments

# Manu said on 26 October, 2009 02:03 AM

what if we need to allow only alphabets???  nd exclude all numbers  ???

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Breaking News
  • Find a Job
  • Social Bookmarking
    Tidebuy Reviews
    Online Shopping
    asp.net hosting
    UK online local dating