Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All,


Name,Items,Price,Total
Alice,5000,14.15,"70,750.00 "
Bunny,14,15.50,217.00
Carrie,100,14.80,"1,480.00"

The above is a csv file which i need to upload the data into database.
But the issue is i want to restrict or show a message to the user if the data inside the field contains comma with it(i.e., the total value in the first row is 70,750.00)So if i find any value of such kind i should restrict the file from uploading .How can i fix this issue.

Thanks
Posted
Updated 14-Aug-12 5:53am
v2
Comments
[no name] 14-Aug-12 11:56am    
What "issue" is there to fix? If field.Contains(",") then show a message or do your restriction....

You could restrict uploading, you better let it upload, validate against your rules, and give feedback. Actually I don't understand what is wrong with the thousand separator, but this is your code. If this is the only rule, you can simply parse it as text, and look for "\d+,.*?" regular expression matches.
 
Share this answer
 
Comments
Аslam Iqbal 14-Aug-12 14:33pm    
Good reply. 5:)
This is not perfect. Might find a better regular expression, but it should work for you:

C#
string re2 = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)";
var regex = new System.Text.RegularExpressions.Regex(re2);
var str = "Alice,5000,14.15,\"70,750.00 \", \"I have a \"\" in this\"";
var results = regex.Matches(str);
var arrayResults = results.Cast<Match>().Select(m => m.Groups[0].Value).ToArray();
foreach (var match in results)
{
    if (match.ToString().IndexOf(",") > -1)
        MessageBox.Show(string.Format("Found string '{0}' with comma.", match.ToString()));
}
 
Share this answer
 
If you are going to construck regular expressions you should try the software in this article:
Expresso - A Tool for Building and Testing Regular Expressions[^]

Menaing it would be difficult or not impossible to understand other peoples RegEX, as you should always construckt them yourself, unless you understand every bit in them.
 
Share this answer
 

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