Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi friends,

I want to validate a textbox field that has following restrictions:

All Numbers can be allowed
At least one letter allowed
No All Characters Allowed

Pls help me

shreeniwas kushwah
Posted
Comments
Kornfeld Eliyahu Peter 23-Sep-14 2:39am    
Learn regex...

Try to use this Regular Expression.

^[0-9]+$

and let me Know, is it working or not..
 
Share this answer
 
All Numbers can be allowed
A number can have zero or more digits in front of a single period (.) and it can have zero or more digits following the period. Perhaps: [0-9]*\.[0-9]* will do ...

A number may not contain a period at all. So, revise the previous expression to:
[-+]?[0-9]*\.?[0-9]*


At least one letter allowed
Basically this means:
Zero or more digits;
One alpha character;
Zero or more alphanumeric characters.
^\d*[a-zA-Z][a-zA-Z0-9]*$ 


No All Characters Allowed
Explanation:
^ matches the start of the string.
[A-Za-z]* matches 0 or more letters (case-insensitive) -- replace * with + to require 1 or more letters.
, matches a comma followed by a space.
$ matches the end of the string, so if there's anything after the comma and space then the match will fail.
^[A-Za-z]*, $


Ex:
XML
<asp:TextBox ID="txtName" runat="server"/>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    <asp:RegularExpressionValidator ID="regexpName" runat="server"
                                    ErrorMessage="This expression does not validate."
                                    ControlToValidate="txtName"
                                    ValidationExpression="^[A-Za-z]*, $" />


Get more Info[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900