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
3 Answers
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.
^[0-9]*$and see other questions that already do have an answer.