Click here to Skip to main content
15,885,842 members
Home / Discussions / C#
   

C#

 
AnswerRe: MessageBoxManager .dll using Pin
ormonds28-Dec-17 9:56
ormonds28-Dec-17 9:56 
GeneralRe: MessageBoxManager .dll using Pin
BillWoodruff29-Dec-17 14:51
professionalBillWoodruff29-Dec-17 14:51 
AnswerRe: MessageBoxManager .dll using Pin
BillWoodruff29-Dec-17 14:56
professionalBillWoodruff29-Dec-17 14:56 
QuestionIssue while parsing HTML string in C# Pin
Mateen1127-Dec-17 18:59
Mateen1127-Dec-17 18:59 
AnswerRe: Issue while parsing HTML string in C# Pin
OriginalGriff27-Dec-17 20:05
mveOriginalGriff27-Dec-17 20:05 
QuestionExtracting a node from Json File in C# Pin
Sandeep Vemulapalli27-Dec-17 11:10
Sandeep Vemulapalli27-Dec-17 11:10 
AnswerRe: Extracting a node from Json File in C# Pin
Nathan Minier28-Dec-17 1:52
professionalNathan Minier28-Dec-17 1:52 
QuestionC # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 4:16
Damian Bz27-Dec-17 4:16 
Hi All,
I have just started to self learn C#, and have written my first basic application.

It is pretty simple,it has 8 text boxes that accept numerical values, which it then does some calculations on and displays 3 textbox outputs.

I worked out the calculations using Excel then translated that into C# and it all works ! as long as no one puts the wrong numbers in !

What I am trying to achieve is to validate four types of inputs:

One set of text boxes can accept whole numbers and no letters from 1 to 1000
One set of text boxes can accept whole numbers and no letters from 1 to 100

On these two types of textboxes, I have this code:

private void totalsize_TextChanged(object sender, EventArgs e)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(totalsize.Text, "[^0-9]"))
    {
        MessageBox.Show("Please enter only full numbers i.e. 70  NOT  65.5");
        totalsize.Text = totalsize.Text.Remove(totalsize.Text.Length - 1);
        totalsize.Focus();
    }
}


From what I understand the regex is basically checking that the first character is not non numerical, so it sort of does one part of what I needed it to do.

I then have another input that I want to be able to validate: numbers only again, but only allow a single number with a single decimal point i.e. 7.2 3.6 but not 7.22222 or 70 so anything from 1 to 9 including a single decimal point.

C#
private void nicstrength_TextChanged(object sender, EventArgs e)
{
        if (System.Text.RegularExpressions.Regex.IsMatch(nicstrength.Text, "insert jibberish here"))
        {
            MessageBox.Show("Please enter only numbers.");
            nicstrength.Text = nicstrength.Text.Remove(nicstrength.Text.Length - 1);
            nicstrength.Focus();
        }

    }


The last input I want to validate will be again numbers only but from 0.n to 2.n , so 0.6 or .6 - 1.2 or 2 (2 or above will probably not ever get entered)

C#
private void finalnicstrength_TextChanged(object sender, EventArgs e)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(finalnicstrength.Text, @"^[1-9]\d*(\.\d+)?$"))
    {
        MessageBox.Show("Please enter only numbers.");
        finalnicstrength.Text = finalnicstrength.Text.Remove(finalnicstrength.Text.Length - 1);
        finalnicstrength.Focus();
    }
}


I have tried zillions of combinations found on the net, plus I've used a couple of regex generators (frustratingly, some say a regex works and some say it doesn't - but none work when I put them into my code !)
I've learnt that I either need to put an @ in front of it, or use double \\ or whatever for C# to know what it is, but i'm just really struggling with this.
I am dyslexic (not looking for sympathy) so looking at regex makes my head explode !

Please can someone help me with the last two types at the very least, I know the top two are not 100% correct, but they at least stop letters and decimal points being entered.

Many thanks for reading, and even more for helping me ! Smile | :)

Damian
AnswerRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 5:00
mveOriginalGriff27-Dec-17 5:00 
GeneralRe: C # + Regex help (for a newbie) Pin
Nathan Minier27-Dec-17 6:10
professionalNathan Minier27-Dec-17 6:10 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:23
mveOriginalGriff27-Dec-17 6:23 
GeneralRe: C # + Regex help (for a newbie) Pin
Nathan Minier27-Dec-17 6:34
professionalNathan Minier27-Dec-17 6:34 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:40
mveOriginalGriff27-Dec-17 6:40 
GeneralRe: C # + Regex help (for a newbie) Pin
Nathan Minier27-Dec-17 6:56
professionalNathan Minier27-Dec-17 6:56 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:24
Damian Bz27-Dec-17 6:24 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:28
mveOriginalGriff27-Dec-17 6:28 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:18
Damian Bz27-Dec-17 6:18 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff27-Dec-17 6:26
mveOriginalGriff27-Dec-17 6:26 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 6:29
Damian Bz27-Dec-17 6:29 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 7:37
Damian Bz27-Dec-17 7:37 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz27-Dec-17 9:15
Damian Bz27-Dec-17 9:15 
GeneralRe: C # + Regex help (for a newbie) Pin
Richard MacCutchan27-Dec-17 21:56
mveRichard MacCutchan27-Dec-17 21:56 
GeneralRe: C # + Regex help (for a newbie) Pin
OriginalGriff28-Dec-17 1:51
mveOriginalGriff28-Dec-17 1:51 
GeneralRe: C # + Regex help (for a newbie) Pin
Damian Bz28-Dec-17 6:59
Damian Bz28-Dec-17 6:59 
AnswerRe: C # + Regex help (for a newbie) Pin
Gerry Schmitz28-Dec-17 8:17
mveGerry Schmitz28-Dec-17 8:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.