Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regular expression validator to check if string has number

I am trying to implement asp.net regularexpression validator and want to check if string contains number or not. Its ok to have string containing number only

I tried below but its not working

<asp:RegularExpressionValidator ID="rfvMyCode" runat="server"  ValidationGroup="ValidationSave" ControlToValidate="txtMyCode"
                ErrorMessage="String must have a Number" ForeColor="#FF3300" ValidationExpression="^[a-zA-Z0-9]+$"></asp:RegularExpressionValidator>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to validate that the string must contain at least one number, you can use .*\d.* regex regex

<asp:RegularExpressionValidator ID="rfvMyCode" runat="server" ValidationGroup="ValidationSave"
    ControlToValidate="txtMyCode" ErrorMessage="String must have a Number" ForeColor="#FF3300"
    ValidationExpression=".*\d.*"></asp:RegularExpressionValidator>

.* means "0 or more of any character"

\d matches any single digit [0-9]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading