.Net Multiline expression to limit integers

I have a problem with a very simple regex.

I want to limit the entry in a multiline text field to integers only. My regular expression works fine in single-line mode (for a single TextBox line that does not use a multi-line parameter), but allows alpha characters to creep in multi-line mode, but only after entering a new line.

My code (C #) is something like:

Regex regExpr = new Regex("^(\d*)$", RegexOptions.Multiline) return regExpr.IsMatch(testString); 

I want the following examples to be valid:

 1 1\\n 1\\n2\\n3 

I want the following to be invalid

 A A1\\n2 1\\n2\\nA3 

Thanks in advance.

+4
source share
3 answers

What about

 (\d?\\n*)? 
0
source

You can match numbers and newlines with:

 Regex regExpr = new Regex("[\d\n]*", RegexOptions.Multiline) 

This will match any number of digits and a new line. If you just want to make sure that the text you enter does not have a NON digit, use

 Regex regExpr = new Regex("[\D\S]", RegexOptions.Multiline) 

and if it matches, then you have an illegal entry.

+1
source

Do you want to destroy the alpha or just send it back to re-enter the information?

to remove, simply replace the search with an empty result.

[^\d\n]

To check if there is anything but a number and \ n do the same only with the first entry error, send and send the page to the user.

Since I do not know which .NET you are using, I can give general principles.

0
source

All Articles