-2

Hello i want to validate that user can enter only digits and the digit may be a integer or decimal but not alphabets just decimal no how can i write the regular expression for this. help me thank you

Hasnain
  • 1
  • 3
  • Try `^[0-9]*$` and see [other](https://stackoverflow.com/questions/19715303/regex-that-accepts-only-numbers-0-9-and-no-characters) [questions](https://stackoverflow.com/questions/273141/regex-for-numbers-only) that already do have an answer. – Thomas Flinkow Mar 02 '18 at 06:45

3 Answers3

0

Regular expression for integer or decimal

pattern: /^\d+(.\d{1,2})?$/

0

You might want to take internationalisation into account if you are on asp and deploying a website.

using System;
using System.Globalization;

    //from: https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimalseparator(v=vs.110).aspx
    // Gets a NumberFormatInfo associated with the en-US culture.
    NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
    // Displays the same value with a blank as the separator.
    string seperator = nfi.NumberDecimalSeparator;

    string seperatorRegex = "";

    foreach(char chr in seperator.ToCharArray())
    {
        seperatorRegex += $"[{chr}]?";
    }

    string Pattern = $@"\d+{seperatorRegex}\d*";

    //do matching....

If not other answers are fine if not even cleaner.

Robin B
  • 1,066
  • 1
  • 14
  • 32
0

You can use Character Classes.

[\d]*.?[\d]*

[] - Character classes.

\d - Same as [0-9].

Additionally you can use named groups to extract integer and fraction

(?<integer>[\d]*).?(?<fraction>[\d]*)

(?) - capturing group.

? - Same as {0,1}.

Shyam Poovaiah
  • 334
  • 1
  • 2
  • 9