-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

1
  • Try ^[0-9]*$ and see other questions that already do have an answer. Commented Mar 2, 2018 at 6:45

3 Answers 3

0

Regular expression for integer or decimal

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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}.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.