Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, everyone. I am working on vb mvc application. In my model I have these two fields:

Public Property IncludedColumns As Integer


Public Property Ladder As String


I want to validate "IncludedColumns" with regExp to get only integer values.
"Ladder" is a string field which must receive values like this: "23,47,71,95". I want to validate "Ladder" with regExp so if after each two digits comma is missing to have error message and if there are any spaces to have error message and if there are any other symbols different than digits and commas to have error message.
Please help me with these two regExp.

What I have tried:

I was searching for the digit regExp but without success.
Posted
Updated 7-Feb-19 3:31am

Regular expressions work only on text, you cannot use them to validate integer values. For sample regexes see RegExr: Learn, Build, & Test RegEx[^]
 
Share this answer
 
Quote:
I want to validate "IncludedColumns" with regExp to get only integer values.


I'd say: try to add value different than integer to find out what happens...

Quote:
"Ladder" is a string field which must receive values like this: "23,47,71,95". I want to validate "Ladder" with regExp so if after each two digits comma is missing to have error message


Try this and change to your needs:

VB
Dim ladders As String()= {"23,47,71,95", "23,47.71,95", "23,47,71 95"}

Dim pattern As String = "^\d{2},\d{2},\d{2},\d{2}$"
For Each lad As String In ladders
	Console.WriteLine("{0} => {1}", lad, Regex.IsMatch(lad, pattern))
Next

C#
string[] ladders = {"23,47,71,95", "23,47.71,95", "23,47,71 95"};

string pattern = @"^\d{2},\d{2},\d{2},\d{2}$";
foreach(string lad in ladders)
{
	Console.WriteLine("{0} => {1}", lad, Regex.IsMatch(lad, pattern));
}


Result:
23,47,71,95 => True
23,47.71,95 => False
23,47,71 95 => False
 
Share this answer
 
v3
Quote:
Please help me with these two regExp.

You need to be specific about what is allowed and what is not, with examples.
Note that RegEx only tells you if a string is valid or not, your code have to throw error message when necessary.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900