Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're looking for a concise ASP.NET solution, you can use the RegularExpressionValidator control to restrict the contents of an ASP.NET TextBox. You don't have to write any server or client side code other than the page tag definition and the regular expression used for validation. Validation will get checked at both sides for you.</p> <p>For example:</p> <pre><code>&lt;asp:TextBox id="txtItem" runat="server" MaxLength="50"&gt;&lt;/asp:TextBox&gt; &lt;asp:RegularExpressionValidator id="SomeFieldValidator" runat="server" CssClass="SomeClass" ControlToValidate="txtItem" ErrorMessage="This field only accepts ASCII input." Display="Dynamic" ValidationExpression="^[A-Za-z0-9]*$"&gt;&lt;/asp:RegularExpressionValidator&gt; </code></pre> <p>In this snippet, <strong>txtItem</strong> is the TextBox that needs validation. the <strong>SomeFieldValidator</strong> control is linked using the <em>ControlToValidate</em> attribute to the <strong>txtItem</strong> control. The <em>ValidationExpression</em> attribute is the regular expression that is used to enforce the contents of the TextBox. Per the documentation, this expression needs to be written to be compatible with both JScript and .NET Regex regular expressions. Also, I've only set the regular expression here to alpha-numeric characters. You might want to consider using something like <strong>^[\w\s]*$</strong> instead- if you are actually interested in printable characters instead of just ASCII.</p> <p>A side benefit to using this technique is that you can also insert these validators into .aspx pages of updatable applications, without requiring anything other than an App Pool restart.</p> <p>Also handy is the <strong>RequiredFieldValidator</strong> control. You can link these with <strong>TextBox</strong> controls that also have a <strong>RegularExpressionValidator</strong> attached. These handle the case of requiring input in a <strong>TextBox</strong>.</p> <p>Here are some reference links.</p> <p><a href="http://msdn.microsoft.com/en-us/library/eahwtc9e.aspx" rel="nofollow noreferrer" title="RegularExpressionValidator at MSDN">RegularExpressionValidator Control at MSDN</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/5hbw267h.aspx" rel="nofollow noreferrer">RequiredFieldValidator Control at MSDN</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/20bw873z.aspx" rel="nofollow noreferrer">Character Classes at MSDN (for Regular Expressions)</a></p> <p><a href="http://www.regular-expressions.info/javascript.html" rel="nofollow noreferrer">JavaScript RegExp Object Reference</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload